我正在尝试构建并运行几个月前在wp7上运行良好的旧应用程序,但是http客户端都没有为我工作。
I'm trying to build and run old application which worked fine several month ago on wp7, however none of http clients works for me.
首先,我m尝试 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("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看起来像手机不能到达网络...虽然:
Looking like phone cant reach network at all... Though:
EDIT1: here here here / a>类似的问题,添加
found here a similar issue, added
client.Headers["Content-Type"] = "application/x-www-form-urlencoded"; client.Encoding = Encoding.UTF8;但仍然出现同样的错误。
but still getting same error.
EDIT2:制作了一个新的wp7项目,添加了Bcl.Async和HttpClient。仍然是同样的问题。
made a fresh wp7 project, added Bcl.Async and HttpClient. Still the same problem.
EDIT3:昨晚研究:
last night research:
在wp7设备上运行正常:
on wp7 device this works ok:
var client = new RestClient("example"); var request = new RestRequest(String.Empty, Method.GET); client.GetAsync(request, (_response, _handle) => { var resource = _response; var content = resource.Content; });但是当我切换到我的服务器时,它会在wp7设备上抛出NotFound异常。在wp8设备上,它返回正确的结果。
But when i'm switching to my server, it throws NotFound exception on wp7 device. On wp8 device it returns correct result.
推荐答案重置为出厂设置有帮助。
Resetting to factory settings helped.
更多推荐
wp7上的网络错误
发布评论