如何延长http响应的时间长度

时间:2014-02-18 16:09:45

标签: c# asp.net asp.net-mvc asp.net-mvc-3 httpresponse

阅读本文,但没有看到如何实际延长响应的时间限制: https://devcenter.heroku.com/articles/request-timeout

 string responseContents = string.Empty;

 //get web request to wait 5 mins

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

 byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(message);
 request.Method = "POST";
 request.ContentType = "text/xml;charset=utf-8";
 request.ContentLength = requestBytes.Length;

 using (Stream requestStream = request.GetRequestStream())
 {
     requestStream.Write(requestBytes, 0, requestBytes.Length);
     requestStream.Close();
 }

 using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
 {
     using (StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default))
     {
         responseContents = sr.ReadToEnd();
     }
 }

问题在于,服务器中发生了很多事情并且需要时间来返回响应,具体取决于文件大小。

2 个答案:

答案 0 :(得分:3)

HttpWebRequest.Timeout应该诀窍:

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.timeout%28v=vs.110%29.aspx

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = (int)TimeSpan.FromMinutes(5).TotalMilliseconds;

(...)

答案 1 :(得分:1)

您也可以在web.config中执行此操作

<system.web>
    <httpRuntime executionTimeout="180" />
</system.web>

编辑:如果您只想为特定控制器设置它 -

<location path="ControllerName/ActionName">
    <system.web>
        <httpRuntime executionTimeout="180"/>
    </system.web>
</location>