Time out with a .NET WebRequest on IIS 6.0

旧时模样 提交于 2019-12-08 04:54:46

问题


In my ASP.NET 2.0 web service I am trying to access Google API to translate some texts. Following code does this right :

string result = "";

// create the web request to the Google Translate REST interface
System.Net.WebRequest oRequest = System.Net.WebRequest.Create("http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q="
    + System.Web.HttpUtility.UrlEncode("Text to translate") + "&langpair=" + "en" + "%7C" + "fr");

// make the web call
System.Net.WebResponse oResponse = oRequest.GetResponse();

// grab the response stream
System.IO.StreamReader oReader = new System.IO.StreamReader(oResponse.GetResponseStream());

// put the whole response in a string
string sContent = oReader.ReadToEnd();

// parse the string into the litJSON simple object model
JsonData oData = JsonMapper.ToObject(sContent);

// write out the translated text
result = oData["responseData"]["translatedText"].ToString();

(JsonData is from the DLL LitJson --> http://litjson.sourceforge.net)

This works just fine as long as I am on the ASP.NET development server. But as soon as I put my Web service to my IIS 6.0 server, I get "The operation has timed out" errors in my client.

With a System.Net.WebClient I get the same timeout.

Is there any setting on IIS that forbids web requests ?


回答1:


In the development web server the timeout is set to infinite. When you move to IIS6 timeout values are respected and then your request takes longer than the default and then times out. On your oRequest object (System.Net.HttpWebRequest object) you can change the timeout value by setting the Timeout property. The default timeout is 100,000 milliseconds (100 seconds).

oRequest.Timeout = 300000;  //300,000 milliseconds or 300 seconds or 5 minutes  


来源:https://stackoverflow.com/questions/1674449/time-out-with-a-net-webrequest-on-iis-6-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!