Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read httpWebRequest's request stream in c#, i got error" the stream is not readable"?

I want to read the request stream from a custom HttpWebRequest class that inherits from HttpWebRequest and I have tried to read the request stream in different stages but still not sure how to achieve that in the class.

This custom HttpWebRequest is used to serialize a soap message and I want to know what request has been sent in string format. I also implemented custom HttpRequestCreator, HttpWebResponse but still can't find a place/stage from which I can read the request stream.

If i output everything in a MemoryStream then copy the content to request stream, anyone knows which stage I can do it? In the constructor, BeginGetRequestStream, EndGetRequestStream or GetRequestStream?

like image 261
sam Avatar asked Feb 15 '10 03:02

sam


People also ask

How do I read Httpwebresponse response?

If you call the GetRequestStream method, you must use the GetResponse method to retrieve the response. If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.

How HttpWebRequest works?

HttpWebRequest exposes common HTTP header values sent to the Internet resource as properties, set by methods, or set by the system; the following table contains a complete list. You can set other headers in the Headers property as name/value pairs.

What is GetResponseStream C#?

The GetResponseStream method returns the data stream from the Internet resource.

What does GetRequestStream do?

The GetRequestStream method returns a stream to use to send data for the HttpWebRequest and outputs the TransportContext associated with the stream. After the Stream object has been returned, you can send data with the HttpWebRequest by using the Stream.


1 Answers

The "stream is not readable" will result if there is an error return code, like 404, 503, 401, and so on. It's likely you haven't checked your status code.

Something like this works if the content is text:

public string DownloadString(string uri, out int status)
{
    string result= null;
    status = 0;
    HttpWebResponse response= null;
    try
    {
        HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(uri);
        // augment the request here: headers (Referer, User-Agent, etc)
        //     CookieContainer, Accept, etc.
        response= (HttpWebResponse) request.GetResponse();
        Encoding responseEncoding = Encoding.GetEncoding(response.CharacterSet);
        using (StreamReader sr = new StreamReader(response.GetResponseStream(), responseEncoding))
        {
            result = sr.ReadToEnd();
        }
        status = (int) response.StatusCode;
    }
    catch (WebException wexc1)
    {
        // any statusCode other than 200 gets caught here
        if(wexc1.Status == WebExceptionStatus.ProtocolError)
        {
            // can also get the decription: 
            //  ((HttpWebResponse)wexc1.Response).StatusDescription;
            status = (int) ((HttpWebResponse)wexc1.Response).StatusCode;
        }
    }
    finally
    {
        if (response!= null)
            response.Close();
    }
    return result;
}
like image 80
Cheeso Avatar answered Oct 20 '22 00:10

Cheeso