REST client, The remote server returned an error: NotFound.

RafalPudelkoRafalPudelko PLMember
edited September 2016 in Cross Platform with Xamarin

I have a problem with open WebResponse, 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.

//Andorid code: 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); }

Posts

  • kentuckerkentucker USMember ✭✭✭✭✭

    I would recommend using the HttpClient instead of the HttpWebRequest. It works better in a pcl

Sign In or Register to comment.