12

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?

2
  • Please clarify your question if you want to avoid downvotes. Also if you want to get a good answer. Commented Feb 15, 2010 at 4:11
  • Please post your code, without that it is difficult to say what is going on. Commented Feb 16, 2010 at 20:01

3 Answers 3

13

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; } 
Sign up to request clarification or add additional context in comments.

1 Comment

The question is about the request stream, not the response one.
11

Your question is unclear.

If you're trying to read an HttpWebRequest's request stream after other code has written to the stream (a POST request), it's not possible. (The stream is sent directly to the server and is not stored in memory)

Instead, you'll need to expose your own MemoryStream, then write it to the HttpWebRequest's request stream wen you're ready to send the request.

1 Comment

thanks very much for the response, could u let me know which stage/method i can expose my memorystream ? thanks again
-5

Try using Fiddler. Worked for me.

Fiddler:

  • is a Web Debugging Proxy
  • logs all HTTP(S) traffic between your computer and the Internet
  • allows you to inspect all HTTP(S) traffic, set breakpoints, and "fiddle" with data
  • is freeware
  • can debug traffic from all almost all apps (web browsers and more)

1 Comment

that has nothing to do with this. This is a request in an ASP page (or just c#) to an external URL with the intention of using the data in the original request in your application. Even if that were not the case, I'm not going to but a proxy on my production server.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.