Why does httpWebRequest.GetResponse() timeout when stream is not used? - c#

I just ran into a very strange behavior that I would like to understand. I am reading an image via HttpWebRequest. The following code raises a timeout exception:
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.Timeout = 5000;
HttpWebResponse httpWebReponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream stream = httpWebReponse.GetResponseStream();
Please note that stream is not used anywhere else afterwards. However, if I add the following line:
Image image = Image.FromStream(stream);
Also here note that image is not used anywhere later on. Can anyone explain why I get the timeout in the first case?

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.

Connection time out on REST API call

I have class in c# which makes POST call to a REST API. BUt it gives me a 'connection time out' error with error code as 10060. In the POST call I am trying to make some transaction adjustments in the client's system. When I use Fiddler or Postman to make the api call, the request seems to go through but not from the c# class. Can you see where I am going wrong?
Below is my sample code.
HttpWebRequest request = (HttpWebRequest)WebRequest.CreateHttp(clientURL);
byte[] data = Encoding.UTF8.GetBytes(urlParameters);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
using (Stream stream = request.GetRequestStream()) **//exception occurs at this point**
{
stream.Write(data, 0, data.Length);
}
Thanks.
You have to close your webrequest by using "using" or adding a call to the HttpWebRequest.Abort() method
The default timeout for HttpWebRequest is 100 seconds. You can change that value by setting that property (in ms):
request.Timeout = 120000;

HttpWebResponse.ReadTimeout - Timeouts not supported?

We have an issue where on a single instance of our product we receive an InvalidOperationException exception when we attempt to set the ReadTimeout property of an System.Net.HttpWebResponse object.
This issue is occurring only on a single instance, where we have many multiple live sites without this problem. We've tried to recreate the issue locally, to no avail.
The following code illustrates the issue.
Any ideas are greatly welcome.
Thanks
private static XmlReader GenerateReaderFromResponse(HttpWebResponse response, HttpWebRequest request)
{
Stream responseStream = response.GetResponseStream();
responseStream.ReadTimeout = request.Timeout; //This is where the exception is generated - System.InvalidOperationException: Timeouts are not supported on this stream.
using (StreamReader responseReader = new StreamReader(responseStream, System.Text.Encoding.UTF8))
{
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ProhibitDtd = false;
string responseContent = responseReader.ReadToEnd();
return XmlReader.Create(new StringReader(responseContent), readerSettings);
}
}
What you need is the HttpWebRequest.ReadWriteTimeout property.
It specifies the number of milliseconds before the reading (or writing) operation on the response Stream times out, throwing a WebException with Status set to WebExceptionStatus.RequestCanceled.
From the msdn documentation:
The ReadWriteTimeout property is used when writing to the stream
returned by the GetRequestStream method or reading from the stream
returned by the GetResponseStream method.
Specifically, the ReadWriteTimeout property controls the time-out for
the Read method, which is used to read the stream returned by the
GetResponseStream method, and for the Write method, which is used to
write to the stream returned by the GetRequestStream method.
To specify the amount of time to wait for the request to complete, use
the Timeout property.
First, make sure that responseStream and request are not null.
Them, you should make sure that the server did respond to your request after trying to read the response from it.
If you can, please also provide the code that sends the request.

HttpWebRequest is very slow

I am requesting a handler file from another handler file that returns an image, when I request my HttpWebRequest it takes more time to get the response. Here is my code, please help.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpCookie cookie = context.Request.Cookies["ASP.NET_SessionID"];
Cookie myCookie = new Cookie(cookie.Name, cookie.Value);
myCookie.Domain = url.Host;
request.CookieContainer = new CookieContainer();
request.CookieContainer.Add(myCookie);
request.Timeout = 200000;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
responseStream = response.GetResponseStream();
First make sure that your request is getting to the handler as quickly as possible. If not then it is some issue with your network. You can diagnose this with logs or debugging or whatever way you need. Use Fiddler to re-issue requests so you know exactly when it is fired and received.
If it is getting to the server and no processing on there is taking too much time then make sure that you are flushing and closing the response stream when you are finished writing to it. Also probably best to dispose of the responseStream object.
using(var responseStream = response.GetResponseStream()){
// write to the sucker
responseStream.Flush();
responseStream.Close();
}
NB If this is only on the first request (your question/answers are a little confusing) then try work out what exactly is happening at startup of the appdomain. Is there something big in global.asax - or is it doing a lot of DB work?

Question about HttpWebRequest class in .net

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

Categories

Resources