Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My Windows Phone app Get empty response (404 Not Found) Scond time, work's great first time;And always work fine if without SSL

I am building my first windowsPhone 8.1 application ,the role of my application is to create connection with server to get information from it, so I am writing the code to do this process by sending json-rpc request to server to get some information ,I am successful to get it in first time but when I send the second request I am receiving an empty response with 404 error (page not found). But when I call the service without https (http only) it works fine regardless how many time 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 call the URL of service (start with https) on first time I got response with seeked data, but second time I receive an empty response with 404 error (page not found) !! Any help please

like image 759
Bashar Abu Shamaa Avatar asked Jun 04 '14 13:06

Bashar Abu Shamaa


1 Answers

Please try to use 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;
        }

    }
like image 130
Mohammad Diab Avatar answered Sep 30 '22 22:09

Mohammad Diab