Question about HttpWebRequest class in .net - c#

I would like to know two things about the following code:
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
Here are my two questions:
- What is exactly a stream?
- The line myWriter.Write sends an Http Packet with the post information or to do that i have to use a method of HttpWebRequest class?

As already stated a Stream is the usual .NET equivalent of a buffer. It's also almost always used when doing any sort of IO, be it files, pipes, network. Usually to work with a stream you use either StreamReader or StreamWriter.
Your method should be sending a packet correctly. To read a response you would do a similar operation with GetResponseStream.

A stream in .NET can be regarded as kind of a buffer.
It is used in file/http/memory IO

The stream in this case is a buffer which will be sent over network. This buffer is sent when you use GetResponse function

http://msdn.microsoft.com/fr-fr/library/system.net.webresponse.getresponsestream%28VS.80%29.aspx

Related

WCF stops responding after some requests

I have built wcf. it is working well
The issue is when I call it many times it displays the following error:
The server encountered an error processing the request. See server
logs for more details
I configured a WCF Tracing File but it remains always empty. what can be the reason of this sudden stop of the service and how to fix it?
Here is the code that I use at the client's side every 20 seconds:
string url = "http://host/Service.svc/method";
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "GET";
ASCIIEncoding encoding = new ASCIIEncoding();
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
StreamReader loResponseStream =
new StreamReader(webresponse.GetResponseStream(), enc);
string strResult = loResponseStream.ReadToEnd();
loResponseStream.Close();
webresponse.Close();
I fixed the issue. it was due to open database connections. I missed to close, at the server side, the database connections. Thank you for answer
It could be a working memory issue on the server/host. If there's less than 5% available you get no response.

Read a HttpWebRequest stream just before sending it out

I am making a POST request using the System.Net.HttpWebRequest class and want to ensure that the request is well-formed just before I send it out.
So, I want to print out the entire request stream before it goes out.
I am looking at things in Fiddler but I am still interested in knowing if there is a way to programmatically read a request stream before it is sent out.
The trouble is that the request stream is not seekable, and it is not readable either. How do I read it?
So, this thing won't work:
...
accessTokenRequest.Method = "POST";
var accessTokenRequestStream = accessTokenRequest.GetRequestStream();
accessTokenRequestStream.Write(buffer, 0, buffer.Length);
accessTokenRequestStream.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(accessTokenRequestStream))
{
var requestText = reader.ReadToEnd();
Debugger.Break();
Debug.Print(requestText);
}
accessTokenRequestStream.Close();
The request stream is write-only, there is no way to read it.
What you can do, however, is prepare the request body in a MemoryStream, then rewind the MemoryStream and copy it to the request stream.

HttpWebRequest to web-service in c#; how to get data FAST from the response stream?

I am calling a web-service with POST and receiving a 2MB xml.
The problem is that it takes to much time until i can use the data within the Stream.
The response seems to be after 7 secs there, but it takes another 10 sec to read the content(its a string) from response stream.
Stopwatch s = new Stopwatch();
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(MyUri);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = Poststring.Length;
s.Start();
StreamWriter swriter = new StreamWriter(req.GetRequestStream());
swriter.Write(Poststring);
swriter.Close();
// Get the response. 7 sec
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
s.Stop();
Debug.WriteLine("Talking to Web-Service: "+s.ElapsedMilliseconds);
s.Reset();
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content. 10 sec
XmlReader xmlReader = XmlReader.Create(dataStream);
s.Start();
XDocument xdoc = XDocument.Load(xmlReader);
s.Stop();
Debug.WriteLine("Convert stream to some useful data: "+s.ElapsedMilliseconds);
output in milliseconds
Talking to Web-Service: 6595
"Convert" stream to some useful data: 10772
Why does it take like 10 sec to read the content??
Is there stil some communication with the web-service or waiting for data when content is read? Its just a simple textfile (xml) with about 2MB. I thought that those 2 MB were transfered within the 6596 milliseconds. Because when i call that service with my browser, the xml content is shown in 6-7 sec.
The time for Talking to Web-Service is ok, but what is going on in those 10772 milliseconds?
Edit: The problem is stil there. I get different answers and they contradict each other.
Add following
httpWebRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
resulting in:
Talking to Web-Service: 6595
"Convert" stream to some useful data: 256
Now i have the same performance like in a browser!
XDocument xdoc = XDocument.Load(response.GetResponseStream(), LoadOptions.None);
Avoid the XmlReader.Create and use the XDocument.Load(Stream, LoadOptions) overload.
http://msdn.microsoft.com/en-us/library/cc838321.aspx
GetResponse will not return the full response stream. GetResponse will send your request and return an HttpWebResponse object based on the header information from the response. HttpWebResponse also has an associated stream from which you can read the full response body. This is precisely what you are doing.
I suspect that the 7-second delay you are seeing when you call GetResponse is a delay on the server in generating the XML document and sending a response. The further 10-second delay on XmlReader.Create is from reading the response stream (ie, downloading the file).
Is the XML generated dynamically? 7 seconds is not a terribly long time for an HTTP response, depending of course on your server location, quality, etc.

Understanding WebRequest

I found this snippet of code here that allows you to log into a website and get the response from the logged in page. However, I'm having trouble understanding all the part of the code. I've tried my best to fill in whatever I understand so far. Hope you guys can fill in the blanks for me. Thanks
string nick = "mrbean";
string password = "12345";
//this is the query data that is getting posted by the website.
//the query parameters 'nick' and 'password' must match the
//name of the form you're trying to log into. you can find the input names
//by using firebug and inspecting the text field
string postData = "nick=" + nick + "&password=" + password;
// this puts the postData in a byte Array with a specific encoding
//Why must the data be in a byte array?
byte[] data = Encoding.ASCII.GetBytes(postData);
// this basically creates the login page of the site you want to log into
WebRequest request = WebRequest.Create("http://www.mrbeanandme.com/login/");
// im guessing these parameters need to be set but i dont why?
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
// this opens a stream for writing the post variables.
// im not sure what a stream class does. need to do some reading into this.
Stream stream = request.GetRequestStream();
// you write the postData to the website and then close the connection?
stream.Write(data, 0, data.Length);
stream.Close();
// this receives the response after the log in
WebResponse response = request.GetResponse();
stream = response.GetResponseStream();
// i guess you need a stream reader to read a stream?
StreamReader sr = new StreamReader(stream);
// this outputs the code to console and terminates the program
Console.WriteLine(sr.ReadToEnd());
Console.ReadLine();
A Stream is a sequence of bytes.
In order to use text with a Stream, you need to convert it into a sequence of bytes.
This can be done manually with the Encoding classes or automatically with StreamReader and StreamWriter. (which read and write strings to streams)
As stated in the documentation for GetRequestStream,
You must call the Stream.Close method to close the stream and release the connection for reuse. Failure to close the stream causes your application to run out of connections.
The Method and Content-* properties reflect the underlying HTTP protocol.

Is there any C# equivalent to the Perl's LWP::UserAgent?

In a project I'm invovled in, there is a requirment that the price of certain
stocks will be queryed from some web interface and be displayed in some way.
I know the "query" part of the requirment can be easily implemented using a Perl module like LWP::UserAgent. But for some reason, C# has been chosen as the language to implement the Display part. I don't want to add any IPC (like socket, or indirectly by database) into this tiny project, so my question is there any C# equivalent to the Perl's LWP::UserAgent?
You can use the System.Net.HttpWebRequest object.
It looks something like this:
// Setup the HTTP request.
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.google.com");
// This is optional, I'm just demoing this because of the comments receaved.
httpWebRequest.UserAgent = "My Web Crawler";
// Send the HTTP request and get the response.
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
// Get the HTML from the httpWebResponse...
Stream responseStream = httpWebResponse.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string html = reader.ReadToEnd();
}
I'm not sure, but are you simply trying to make an HTTP Request? If so, you can use the HttpWebRequest class. Here's an example http://www.csharp-station.com/HowTo/HttpWebFetch.aspx
If you want to simply fetch data from the web, you could use the WebClient class. It seems to be quite good for quick requests.

Categories

Resources