我使用以下代码从实时服务器获取数据......
try
{
var httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(url_builder.ToString()));
httpReq.BeginGetResponse((ar) =>
{
var request = (HttpWebRequest)ar.AsyncState;
using (var response = (HttpWebResponse)request.EndGetResponse(ar))
{
//Enter Code here............................
response.Close();
}
});
}, httpReq);
}
catch (Exception ex)
{
Global.EmailExceptionMessage(ex);
}
获得内部异常,即
内部异常:在System.Net.WebConnection.HandleError(WebExceptionStatus st,System.Exception e,System.String where) 在System.Net.WebConnection.ReadDone(IAsyncResult结果)
答案 0 :(得分:2)
您可以尝试使用这一个代码来获取httpresponse。
HttpWebRequest request = WebRequest.Create("http://google.com") as HttpWebRequest;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
WebHeaderCollection header = response.Headers;
var encoding = ASCIIEncoding.ASCII;
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
string responseText = reader.ReadToEnd();
}