c# – 使用Windows 8 Phone App的HttpWebRequest

当我尝试制作HttpWebRequest时,它返回System.Net.ProtocolViolationException错误.

private void txtGo_Click(object sender, RoutedEventArgs e)
    {
        WebRequest client =
            WebRequest.Create("http://api.worldweatheronline.com/free/v1/weather.ashx?q=London&format=json&num_of_days=5&key=jdbcn8yuwebwybxjpqzzxyhy");
        client.ContentType = "application/json";
        client.BeginGetResponse(ReadWebRequestCallBack, client);
    }

    private void ReadWebRequestCallBack(IAsyncResult callBackResult)
    {
        var myRequest = (HttpWebRequest) callBackResult.AsyncState;
        if(myRequest != null)
        {
            try
            {
                var response = (HttpWebResponse)myRequest.EndGetResponse(callBackResult);
                txtContent.Text = response.StatusCode.ToString();
            }
            catch(WebException ex)
            {
                txtContent.Text = ex.Message;
            }
        }
    }

当我删除此行client.ContentType =“application / json”;下面有一个不同的错误

 {System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult  asyncResult)
  at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.<EndGetResponse>b__d(Object sendState)
  at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at MyClimate.MainPage.ReadWebRequestCallBack(IAsyncResult callBackResult)}
base: {System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.
   at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult as yncResult)
   at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClasse.   <EndGetResponse>b__d(Object sendState)
   at System.Net.Browser.AsyncHelper.<>c__DisplayClass1.<BeginOnUI>b__0(Object sendState)
   --- End of inner exception stack trace ---
   at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
   at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
   at MyClimate.MainPage.ReadWebRequestCallBack(IAsyncResult callBackResult)}
   Response: {System.Net.Browser.ClientHttpWebResponse}
   Status: UnknownError
最佳答案
这里有三个问题.首先删除client.ContentType =“application / json”;因为你的请求方法是GET,而且在GET中ContnetType没有意义.
第二个问题是模拟器的Internet连接检查模拟器虚拟机的此设置.

>在Hyper-V管理器中
>单击“模拟器虚拟机”设置
>单击Windows Phone仿真器外部接口选择虚拟
切换其名称以您的物理网络设备名称开头.

有关更多信息,请参阅Troubleshooting the Windows Phone 8 Emulator.

第三个问题是从UI线程以外的另一个线程更新UI控件(TextBox).

更改

   txtContent.Text = response.StatusCode.ToString();

   Deployment.Current.Dispatcher.BeginInvoke(() =>
   {
      txtContent.Text = response.StatusCode.ToString();
   });

转载注明原文:c# – 使用Windows 8 Phone App的HttpWebRequest - 代码日志