views:

58

answers:

2

Hi,

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();
+2  A: 

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();
SLaks
wow man you are a saviour ? btw just curious, any idea why it happens? i mean how it relates to closing the stream ?
Aneef
The request will not be sent until the request stream is closed. (In case you want to write more to it) If you don't close it yourself, you're relying on the non-deterministic GC, which is always a bad idea.
SLaks
+3  A: 

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>
JoshBerke