Aneef
Aneef

Reputation: 3729

ASP.Net Page HttpWebRequest takes ages to respond

Below is my code (ASP.net web project), but when i debug this asp page it takes ages to retrieve the response ? any idea why is this happening ?

and also the providers of the aURl mentioned to use req.connection="Close" but when i use that throws out an error. (im new to httpwebrequest sigh)

this is the documentation about connection -This value specifies that the connection is not to be a keep-alive connection.

var url = new Uri(@"My URL");
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.KeepAlive = false;      
    req.Method = "POST";
    req.ContentType = "text/xml";
    //This Fails but the documentation asks to use this ??
    //req.Connection = "Close";
     var requestdata = File.ReadAllText(@"D:\request.txt");
    //req.ContentLength = requestdata.Length;
    StreamWriter myWriter = null;
    myWriter = new StreamWriter(req.GetRequestStream());
    myWriter.Write(requestdata);
    HttpWebResponse objResponse = (HttpWebResponse)req.GetResponse();

Upvotes: 1

Views: 456

Answers (2)

JoshBerke
JoshBerke

Reputation: 67068

Try and turn off the auto detection for a proxy. I have seen where the first request made from an application can be order of magnitude slower because of this:

  <system.net>
    <defaultProxy>
      <proxy autoDetect="False"/>
    </defaultProxy>
  </system.net>

Upvotes: 3

SLaks
SLaks

Reputation: 887453

This would happen if you have a slow internet connection, or if the URL is on a slow server.

However, try closing the request stream, like this:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

req.KeepAlive = false;      
req.Method = "POST";
req.ContentType = "text/xml";

using(StreamWriter myWriter = new StreamWriter(req.GetRequestStream())
    myWriter.Write(File.ReadAllText(@"D:\request.txt"));

HttpWebResponse objResponse = (HttpWebResponse)req.GetResponse();

Upvotes: 3

Related Questions