HttpWebRequest with Windows 8 Phone App

When I try to do an HttpWebRequest , it returns a System.Net.ProtocolViolationException error.

 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; } } } 

When I delete this line client.ContentType = "application/json"; , there is another error that is lower

  {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 
+4
source share
3 answers

There are three questions here. First of all, delete client.ContentType = "application/json"; , because your GET and ContnetType request method does not make sense in GET. The second problem is the Internet connection of your emulator, checking this parameter of the Emulator virtual machine.

  • In Hyper-V Manager
  • Click "Emulator Virtual Machine Settings"
  • Click "External Windows Phone Emulator" Select "Virtual" which its name begins with your physical network device name.

For more information, see Troubleshooting Windows Phone 8 Emulator .

enter image description here The third problem is updating the user interface control (TextBox) from a different thread than the user interface thread.

Change

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

to

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

You will receive this System.Net.ProtocolViolationException because you do not have Publish and Receive methods. Hope this helps you.

  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Uri); request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; Touch)"; request.CookieContainer = cookie; request.AllowAutoRedirect = true; // Set the Method property to 'POST' to post data to the URI. request.Method = "POST"; // start the asynchronous operation request.BeginGetRequestStream(new AsyncCallback(CRequest), request); 
+4
source

You can use like this, it will work for your code

just write client.ContentType = "application / x-www-form-urlencoded"; and client.Method = "POST"; in your txtGo_click button and use this method in your code

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

Source: https://habr.com/ru/post/1480272/


All Articles