WebClient没有响应和异常

时间:2015-09-21 08:04:25

标签: c# .net

使用WebClient调用帖子API:

try {
  var webClient = new WebClient(); 
  var result= webClient.UploadString('http://wewe.com/name', {...})
  //log1
} catch(Exception ex) {
  //log2
}

有时我遇到了log1和log2不是日志的问题。 http调用后没有响应和异常?有什么理由吗?

1 个答案:

答案 0 :(得分:-2)

WebException将为您提供答案。只需阅读消息和内部异常。

示例:

string URI = "http://www.myurl.com/post.php";
try
{
    using (WebClient wc = new WebClient())
    {
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        string HtmlResult = wc.UploadString(URI);
    }
catch (WebException webex)
{
            WebResponse errResp = webex.Response;
            using (Stream respStream = errResp.GetResponseStream())
            {
                StreamReader reader = new StreamReader(respStream);
                string text = reader.ReadToEnd();
            }
}