本文整理汇总了C#中System.Net.WebException.GetBaseException方法的典型用法代码示例。如果您正苦于以下问题:C# WebException.GetBaseException方法的具体用法?C# WebException.GetBaseException怎么用?C# WebException.GetBaseException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.WebException
的用法示例。
在下文中一共展示了WebException.GetBaseException方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseWebException
public static string ParseWebException(WebException webException)
{
if (webException == null)
return string.Empty;
var responseContent = GetResponseString(webException.Response);
var response = webException.Response as HttpWebResponse;
if (response != null)
return string.Format(CultureInfo.InvariantCulture, "{0} {1}", response.StatusCode, responseContent).Trim();
if (string.IsNullOrWhiteSpace(responseContent))
return responseContent;
return webException.GetBaseException().Message;
}
示例2: TryWrapException
/// <summary>
/// Will attempt to wrap the exception, returning true if the exception was wrapped, or returning false if it was not (in which case
/// the original exception should be thrown).
/// </summary>
/// <param name="requestContext"></param>
/// <param name="webException"></param>
/// <param name="wrappedException">The wrapped exception (will be set to the webException, if this method returns <c>false</c></param>
/// <returns><c>true</c>, if the authException should be throw, <c>false</c> if the wrapped web exception should be thrown</returns>
public static bool TryWrapException(IOAuthContext requestContext, WebException webException, out Exception wrappedException)
{
try
{
string content = webException.Response.ReadToEnd();
if (content.Contains(Parameters.OAuth_Problem))
{
var report = new OAuthProblemReport(content);
wrappedException = new OAuthException(report.ProblemAdvice ?? report.Problem, webException) { Context = requestContext, Report = report };
}
else
{
wrappedException = new ParsedWebException(content, webException.Message, webException.GetBaseException(), webException.Status, webException.Response);
}
return true;
}
catch
{
wrappedException = webException;
return false;
}
}
示例3: FillStatus
private void FillStatus(ReplicationInfoStatus replicationInfoStatus, WebException e)
{
if (e.GetBaseException() is WebException)
e = (WebException) e.GetBaseException();
var response = e.Response as HttpWebResponse;
if (response == null)
{
replicationInfoStatus.Status = e.Message;
replicationInfoStatus.Code = -1 * (int)e.Status;
return;
}
switch (response.StatusCode)
{
case HttpStatusCode.BadRequest:
string error = GetErrorStringFromException(e, response);
replicationInfoStatus.Status = error.Contains("Could not figure out what to do")
? "Replication Bundle not activated."
: error;
replicationInfoStatus.Code = (int)response.StatusCode;
break;
case HttpStatusCode.PreconditionFailed:
replicationInfoStatus.Status = "Could not authenticate using OAuth's API Key";
replicationInfoStatus.Code = (int)response.StatusCode;
break;
case HttpStatusCode.Forbidden:
case HttpStatusCode.Unauthorized:
replicationInfoStatus.Status = "Could not authenticate using Windows Auth";
replicationInfoStatus.Code = (int)response.StatusCode;
break;
default:
replicationInfoStatus.Status = response.StatusDescription;
replicationInfoStatus.Code = (int)response.StatusCode;
break;
}
}
示例4: ParseStringWebException
public static Exception ParseStringWebException(WebException webException)
{
if (webException == null)
{
return null;
}
try
{
var serializer = new DataContractSerializer(typeof(string));
var stream = webException.Response.GetResponseStream();
var errorMessage = serializer.ReadObject(stream) as string;
return new Exception(errorMessage);
}
catch
{
if (webException.Response is HttpWebResponse)
{
var response = webException.Response as HttpWebResponse;
return new HttpWebException(
string.IsNullOrWhiteSpace(response.StatusDescription) ? webException.Message : response.StatusDescription,
response.StatusCode,
webException);
}
else
{
return webException.GetBaseException();
}
}
}
示例5: ParseXmlWebException
public static Exception ParseXmlWebException(WebException webException)
{
if (webException == null)
{
return null;
}
try
{
var stream = webException.Response.GetResponseStream();
var streamReader = new StreamReader(stream);
var doc = XDocument.Parse(streamReader.ReadToEnd());
return ParseXmlExceptionDocument(doc.Root);
}
catch
{
if (webException.Response is HttpWebResponse)
{
var response = webException.Response as HttpWebResponse;
return new HttpWebException(
string.IsNullOrWhiteSpace(response.StatusDescription) ? webException.Message : response.StatusDescription,
response.StatusCode,
webException);
}
else
{
return webException.GetBaseException();
}
}
}