The correct way to read from the HttpWebResponse and the .NET 4.0 Framework

We are currently facing a ResponseStream reading issue, which we had no problems in the past. Starting with adding .NET 4.0 Framwework to our server last night and assigning IIS to use the new structure, we experience several different exceptions when trying to read responseStream using the following statement (responseStream = httpResponse.GetResponseStream ();). Everything before that works perfectly fine. So, I am looking for changes / improvements regarding how to read the answers. I pasted the code below, which we use, and those exceptions that we experience.

Our environment:

.NET Framework 4.0 Windows Server 2003

THE CODE:

        HttpWebResponse httpResponse;
        Stream responseStream;
        //Accept All Certificate Policy 
        ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };

        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(theUrl));
        httpRequest.Method = "POST";
        httpRequest.KeepAlive = false;
        httpRequest.Timeout = timeOut;


        try
        {
            httpResponse = (HttpWebResponse)httpRequest.GetResponse();
            responseStream = httpResponse.GetResponseStream();
        }

EXCEPTIONS:

'httpResponse.GetResponseStream(). Length' "System.NotSupportedException" long {System.NotSupportedException}

'httpResponse.GetResponseStream(). ' "System.NotSupportedException" long {System.NotSupportedException}

{ " ." } System.SystemException {System.NotSupportedException}


,

Mike

+3
1

, , , . , .

, MemoryStream. .NET 4 :

MemoryStream ms = new MemoryStream();
responseStream.CopyTo(ms);

- (, XML-), Length.

, , , - , , - using - .

+6

Source: https://habr.com/ru/post/1779652/


All Articles