【问题标题】:Network errors on wp7wp7上的网络错误
【发布时间】:2014-05-22 20:56:34
【问题描述】:

我正在尝试构建和运行几个月前在 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

看起来手机根本连不上网络……不过:

  1. 我目前的方法(通过 HttpWebRequest)之前肯定有效
  2. 我绝对可以访问网络,因为其他网络应用或多或少都可以正常工作。
  3. 奖励:wp8 上的同一个应用程序运行良好。所以服务器完全可用,请求有效。

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 设备上它返回正确的结果。

【问题讨论】:

    标签: c# http windows-phone-7 networking restsharp


    【解决方案1】:

    恢复出厂设置有帮助。

    【讨论】:

      猜你喜欢
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 2011-08-27
      • 1970-01-01
      相关资源
      最近更新 更多