0
votes

I have a problem with open WebResponse on WindowsPhone, always when I open WebResponse gets an exception. I have tried many different ways but when I use asynchronous method always gets an exception. Please help me.

exception:

An exception of type 'System.Net.WebException' occurred in System.Net.ni.DLL but was not handled in user code Additional information: The remote server returned an error: NotFound.

private async void btLogin_Click(object sender, RoutedEventArgs e) {
        Uri uri = new Uri("http://ip-address/users/User/jan_kowalski/a");
        HttpWebRequest webRequest = WebRequest.Create(uri) as HttpWebRequest;
        webRequest.Method = "POST";
        webRequest.ContentType = "application/json";

        try {
            using (Stream ss = await webRequest.GetRequestStreamAsync()) {
                await ss.WriteAsync(new byte[0], 0, 0);
                await ss.FlushAsync();
            }

            //using (WebResponse webResponse = await webRequest.GetResponseAsync()) {       // <-- Here there is an exception or...
            //    using (StreamReader reader = new StreamReader(webResponse.GetResponseStream())) {
            //        string data = await reader.ReadToEndAsync();
            //        Log.d(TAG, data);
            //    }
            //}

            webRequest.BeginGetResponse(async (x) => {
                WebResponse webResponse = webRequest.EndGetResponse(x);         // <-- ...or here there is an exception
                StreamReader reader = new StreamReader(webResponse.GetResponseStream());
                string text = await reader.ReadToEndAsync();
                Log.d(TAG, text);
            }, null);

        } catch (WebException exc) {
            Log.e(TAG, exc.Message + "   \n" +
                exc.Source + "\n status:" +
                exc.Status + "\n" +
                exc.Response + "   \n " +
                exc.StackTrace);
        } catch (Exception ex) {
            Log.e(TAG, ex.Message + "\n stackTrace: " + ex.StackTrace);
        }
}

I noticed that Android and IOS have access to more of the methods, synchronous methods. When they use it all works properly, I get the right answer from the server. But synchronous methods are not available on Windows phone!

            Uri uri = new Uri("http://ip-address/users/User/jan_kowalski/a");
            WebRequest webRequest = WebRequest.Create(uri);
            webRequest.Method = "POST";
            webRequest.ContentType = "application/json";
            try {
                using (Stream stream = webRequest.GetRequestStream()) {
                    stream.Write(new byte[0], 0, 0);
                }
                using (WebResponse stream = webRequest.GetResponse()) {
                    using (StreamReader reader = new StreamReader(stream.GetResponseStream())) {
                        string data = reader.ReadToEnd();
                        Console.WriteLine(data);
                    }
                }
            }catch(Exception ex) {
                Log.e(TAG, null, ex);
            }
1

1 Answers

0
votes

I suggest you try HttpClient, the interface is so much cleaner than WebRequest:

var client = new HttpClient();
var res = await client.PostAsync("http://ip-address/users/User/jan_kowalski/a", new StringContent("hello, world"));
if (res.IsSuccessStatusCode)
{
    var response = await res.Content.ReadAsStringAsync();
}

I couldn't reproduce your issue with HttpClient.