wp7上的网络错误
本文关键字:错误 网络 wp7 | 更新日期: 2023-09-27 18:04:45
我正在尝试构建和运行几个月前在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
看起来手机根本无法连接网络…:
- 我目前的方法(通过HttpWebRequest)在 之前肯定有效
- 我肯定有网络访问,因为其他网络应用程序或多或少工作良好。
- 奖励:同样的应用程序在wp8上工作良好。所以服务器完全可用,请求是有效的。
EDIT1:发现这里有一个类似的问题,添加
client.Headers["Content-Type"] = "application/x-www-form-urlencoded";
client.Encoding = Encoding.UTF8;
但仍然得到相同的错误。
EDIT2:创建一个新的wp7项目,添加Bcl。Async和HttpClient。还是同样的问题。
编辑3:昨晚研究:
在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设备上,它返回正确的结果
恢复出厂设置有帮助