每当我在Windows Phone 8中启动应用程序,并发送http GET请求时,我都会得到空响应。这只是第一次请求,来自对同一网址的进一步请求的响应不为空。
以下是代码:
string res = "";
private void ReadUrlAsync(string url)
{
var request = HttpWebRequest.Create(new Uri(url)) as HttpWebRequest;
// request.Accept = "application/json;odata=verbose";
request.BeginGetResponse(ResponseCallback, request);
}
private void ResponseCallback(IAsyncResult asyncResult)
{
HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);
string data;
using (Stream responseStream = response.GetResponseStream())
{
using (var reader = new System.IO.StreamReader(responseStream))
{
data = reader.ReadToEnd();
}
}
res = data;
}
答案 0 :(得分:1)
我使用了一些不同的代码并找到了解决方案:
HttpClient cl = new HttpClient();
return await cl.GetStringAsync(new Uri(url));