Application for Windows Phone Get an empty answer (404 not found) Scond runtime, works great for the first time, and always works fine if without SSL

I am creating my first WindowsPhone 8.1 application, the role of my application is to create a connection to the server to receive information from it, so I write code for this process by sending a json-rpc request to the server, to get some I can get it for the first time, but when I send the second request, I get a blank response with error 404 (page not found). But when I call the service without https (only http), it works fine, no matter how many times I call it!

public async Task<string> GetDataFromServer(string urlToCall, string JSONData,string RR) { string UserName = "XXXXXXX" string Password = "XXX"; using ( var handler = new HttpClientHandler()) { handler.Credentials = new NetworkCredential(UserName, Password); HttpClient client = new HttpClient(handler); HttpResponseMessage response = null; try { response = await client.PostAsync(urlToCall, new StringContent(JSONData.ToString(), Encoding.UTF8, " application/json")); string res = response.Content.ReadAsStringAsync().Result; Windows.UI.Popups.MessageDialog g = new Windows.UI.Popups.MessageDialog(res); await g.ShowAsync(); return res; } catch (Exception ex) { Windows.UI.Popups.MessageDialog g = new Windows.UI.Popups.MessageDialog("Error is : " + ex.Message); g.ShowAsync(); return "Error"; } finally { response.Dispose(); client.CancelPendingRequests(); client.Dispose(); handler.Dispose(); } } } 

Again, when the service url is called (first with https), I received a response with the requested data, but the second time I get an empty response with an error of 404 (page not found) !! Any help please

+7
json json-rpc windows-phone-8
source share
2 answers

Try using this solution.

  public async Task<string> SendJSONData3(string urlToCall, string JSONData) { string UserName = "XXXXXXXXX"; string Password = "XXXXXXXXX"; var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlToCall); httpWebRequest.Credentials = new NetworkCredential(UserName, Password); httpWebRequest.ContentType = "text/json"; httpWebRequest.Method = "POST"; using (var streamWriter = new StreamWriter(await httpWebRequest.GetRequestStreamAsync())) { string json = JSONData; streamWriter.Write(json); streamWriter.Flush(); } var httpResponse = (HttpWebResponse)await httpWebRequest.GetResponseAsync(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); return result; } } 
+3
source share

A few ideas:

  • Do not use the .Result property. Just use await instead to avoid deadlocks.
  • Remove the extra space before the media type parameter " application/json"
  • Enable logging on the web server and see if the second request will go to the server.
  • Get a network trace, such as Wireshark or Fiddler .
  • Try putting WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); into the initialization code as suggested in this.
+1
source share

All Articles