我正在尝试构建和运行几个月前在wp7上运行良好的旧应用程序,但http客户端都不适用于我。
首先,我正在尝试HttpWebRequest
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = HttpMethod.Get;
httpWebRequest.Accept = "application/json";
var response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
它抛出异常
The remote server returned an error: NotFound.
然后我尝试了HttpClient
Uri theUri = new Uri("https://www.google.fi/");
HttpClient aClient = new HttpClient();
aClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
aClient.DefaultRequestHeaders.Host = theUri.Host;
//Post the data
HttpResponseMessage aResponse = await aClient.PostAsync(theUri, new StringContent(postData));
if (aResponse.IsSuccessStatusCode)
{
return aResponse.Content.ToString();
}
else
{
// show the response status code
return "HTTP Status: " + aResponse.StatusCode.ToString() + " - Reason: " + aResponse.ReasonPhrase;
}
它返回
System.Net.HttpStatusCode.NotFound
看起来电话根本无法到达网络......虽然:
EDIT1:found here类似的问题,已添加
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
client.Encoding = Encoding.UTF8;
但仍然得到同样的错误。
EDIT2:制作了一个新的wp7项目,添加了Bcl.Async和HttpClient。仍然是同样的问题。
EDIT3:昨晚研究:
在wp7设备上运行正常:
var client = new RestClient("http://example.com");
var request = new RestRequest(String.Empty, Method.GET);
client.GetAsync(request, (_response, _handle) =>
{
var resource = _response;
var content = resource.Content;
});
但是当我切换到我的服务器时,它会在wp7设备上抛出NotFound异常。在wp8设备上,它返回正确的结果。
答案 0 :(得分:0)
重置为出厂设置有帮助。