locked
GetResponseAsync Throws a "System.NotSupportedException" RRS feed

  • Question

  • I'n my WP 8.1 i have a httpwebrequest that tries to PUT a file to a service,

                HttpWebRequest request = ConstructWebRequest(endpointUri, httpMethod, headers);
    
    
                var buffer = new byte[8192]; // arbitrary buffer size                        
                var requestStream = await request.GetRequestStreamAsync();
    
    
                using (var inputStream = requestBody)
                {
                    var bytesRead = 0;
                    while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        requestStream.Write(buffer, 0, bytesRead);
                    }
                }
    
    
             var webResponse =  await request.GetResponseAsync();
                var t = webResponse;
     

    I have a breakpoint on  the last line, that never gets hit but it runs fine till there. All i get is :

    A first chance exception of type 'System.NotSupportedException' occurred in System.Windows.ni.dll
    A first chance exception of type 'System.NotSupportedException' occurred in System.Windows.ni.dll
    A first chance exception of type 'System.NotSupportedException' occurred in System.Windows.ni.dll
    A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.ni.dll
    A first chance exception of type 'System.NotSupportedException' occurred in mscorlib.ni.dll

    MSDN says the method is supported ... did the .net guys forget to implement it ?

    Thursday, May 1, 2014 7:48 PM

Answers

  • OK, in all of the code samples provided above, there is no reference to closing/ disposing the RequestStream once it is done. You MUST close the Request Stream before calling GetResponseAsync, not doing so will thrown the System.NotSupportedException.

    While the documentation of HttpWebRequest.GetRequestStream clearly mentions about closing the request stream, the documentation of WebRequest.GetRequestStreamAsync has no mention about closing the stream which is why you are seeing the exception.

    I have asked the documentation team to clarify the docs and explicitly mention about closing/disposing the request stream before calling GetResponse.

    The best way is to retrieve the Request stream in a using statement and let the CLR take care of adding the Dispose for you automatically:

                try
                {
                    HttpWebRequest request = WebRequest.Create("http://localhost/time.asp") as HttpWebRequest;
                    request.Method = "POST";
                    var buffer = new byte[8192]; // arbitrary buffer size                        
                    for (int i=0; i < buffer.Length; i++)
                    {
                        buffer[i] = 65;
                    }
                    
                    using (var requestStream = await request.GetRequestStreamAsync())
                    {
                        requestStream.Write(buffer, 0, buffer.Length);
                    }
    
                    using (var webResponse = await request.GetResponseAsync())
                    {
                        StreamReader responseStream = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
                        Debug.WriteLine("Response:" + responseStream.ReadToEnd()); 
                    }                
                }
                catch (Exception oEx)
                {
                    Debug.WriteLine("Exception: " + oEx.Message);
                }


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Tuesday, May 6, 2014 10:56 PM

All replies

  • Hi aviatrix ,

    An easy approach to use PostClient in WP , use this link :

    http://postclient.codeplex.com/

    Regards,


    Pardeep

    • Proposed as answer by BEYOND.COM INC Tuesday, May 6, 2014 8:52 PM
    • Unproposed as answer by aviatrix Tuesday, May 6, 2014 8:59 PM
    Thursday, May 1, 2014 7:56 PM
  • That's not a viable option. 

    Also i'm doing a PUT request with a method that says it's implemented in MSDN docs but throws "not supported" Using another (3-rd party) library won't help me much

    Friday, May 2, 2014 8:44 AM
  • Same problem here with a universal app on Windows Phone
    Saturday, May 3, 2014 7:19 AM
  • Saturday, May 3, 2014 9:57 AM
  • Done.

    I can't help but think there must be an obvious fix for this issue. I can't believe that it would be missing from the SDK. Surely its a fairly common and fundamental function call?

    Saturday, May 3, 2014 7:17 PM
  • Are you setting the http Method header to PUT before you call this?  You can't set a body and use the default GET method in http - and the framework will therefore throw a NotSupported indicating your incompatible method/body situation..

    Darin R.

    Sunday, May 4, 2014 3:56 PM
  • I believe I'm using POST but I'll check later to be sure
    Sunday, May 4, 2014 4:35 PM
  • I am facing the exact same problem.

    HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(uri);
                    webRequest.Method = "POST";
                    webRequest.ContentType = "application/x-www-form-urlencoded";
    
                    Task<Stream> requestTask = webRequest.GetRequestStreamAsync();
                    using (Stream requestStream = requestTask.Result)
                    {
                        requestParameters.WriteToStream(requestStream);
                    }
    
    
                    Task<WebResponse> responseTask = webRequest.GetResponseAsync();
                    using (StreamReader requestReader = new StreamReader(responseTask.Result.GetResponseStream()))
                    {
                        string webResponse = requestReader.ReadToEnd();
                    }

    This code throws NotSupportedException when I call responseTask.Result.GetResponseStream

    Sunday, May 4, 2014 10:19 PM
  • 	    // Create the web request.
                HttpWebRequest webRequest = WebRequest.CreateHttp(serverUrl);
    			webRequest.Method = "POST";
    			webRequest.ContentType = "application/x-www-form-urlencoded";
    			string requestData;
    			if (_channel == null)
    				requestData = "ChannelUri=simulator&Hardware=" + udid;
    			else
    			{
    				requestData = "ChannelUri=" + _channel.Uri + "&Hardware=" + udid;
    				_channel.PushNotificationReceived += OnPushNotification;
    			}
    			if (ScoreManager.isSignedIn())
    				requestData += "&LiveID=" + ScoreManager.getUserID();
    			byte[] channelUriInBytes = Encoding.UTF8.GetBytes(requestData);
    
    			// Write the channel URI to the request stream.
    			try
    			{
    				Stream requestStream = await webRequest.GetRequestStreamAsync();
    				requestStream.Write(channelUriInBytes, 0, channelUriInBytes.Length);
    
    				// Get the response from the server.
    				WebResponse response = await webRequest.GetResponseAsync();
    Yup, mine was POST. Here is the code block I have. 
    Monday, May 5, 2014 6:37 AM
  • Can someone post a "full" repro of the reported problem by uploading the sample project to OneDrive/Dropbox? That way it will be easier to repro and understand the issue.

    Btw, here's my repro project that I created which does not throw the NotSupportedException: http://1drv.ms/1mzvzKN

    If you can upload your version of the repro, it'd be easier to understand.


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog


    Tuesday, May 6, 2014 6:24 PM
  • OK, in all of the code samples provided above, there is no reference to closing/ disposing the RequestStream once it is done. You MUST close the Request Stream before calling GetResponseAsync, not doing so will thrown the System.NotSupportedException.

    While the documentation of HttpWebRequest.GetRequestStream clearly mentions about closing the request stream, the documentation of WebRequest.GetRequestStreamAsync has no mention about closing the stream which is why you are seeing the exception.

    I have asked the documentation team to clarify the docs and explicitly mention about closing/disposing the request stream before calling GetResponse.

    The best way is to retrieve the Request stream in a using statement and let the CLR take care of adding the Dispose for you automatically:

                try
                {
                    HttpWebRequest request = WebRequest.Create("http://localhost/time.asp") as HttpWebRequest;
                    request.Method = "POST";
                    var buffer = new byte[8192]; // arbitrary buffer size                        
                    for (int i=0; i < buffer.Length; i++)
                    {
                        buffer[i] = 65;
                    }
                    
                    using (var requestStream = await request.GetRequestStreamAsync())
                    {
                        requestStream.Write(buffer, 0, buffer.Length);
                    }
    
                    using (var webResponse = await request.GetResponseAsync())
                    {
                        StreamReader responseStream = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
                        Debug.WriteLine("Response:" + responseStream.ReadToEnd()); 
                    }                
                }
                catch (Exception oEx)
                {
                    Debug.WriteLine("Exception: " + oEx.Message);
                }


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Tuesday, May 6, 2014 10:56 PM