ASP.Net页面HttpWebRequest需要很长时间才能响应

时间:2010-02-24 15:59:25

标签: asp.net httpwebrequest

下面是我的代码(ASP.net web项目),但是当我调试这个asp页面时,检索响应需要很长时间?知道为什么会这样吗?

以及提到的aURl的提供者使用req.connection =“Close”,但是当我使用它时会抛出错误。 (我是httpwebrequest 叹息的新手)

  

这是关于的文档   连接 - 此值指定   连接不是一个   保持连接。

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 个答案:

答案 0 :(得分:3)

如果您的互联网连接速度较慢,或者网址较慢,则会发生这种情况。

但是,请尝试关闭请求流,如下所示:

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();

答案 1 :(得分:3)

尝试并关闭代理的自动检测功能。我已经看到,从应用程序发出的第一个请求可能会因此而降低数量级:

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