如果您将Thread.CurrentThread.CurrentCulture更改为与安装(OS区域性)不同的内容,则对HttpWebRequest.GetResponse()的调用将始终超时。
为了澄清这一点,下面是我的实用工具类的内容,它正在进行肮脏的工作:
private static void ReplicateCookies(HttpWebRequest request)
{
if (HttpContext.Current == null)
return;
if (request.CookieContainer == null)
request.CookieContainer = new CookieContainer();
foreach (var cookie in from object key in HttpContext.Current.Request.Cookies.Keys select HttpContext.Current.Request.Cookies[key.ToString()])
request.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, string.IsNullOrEmpty(cookie.Domain)
? HttpContext.Current.Request.Url.Host
: cookie.Domain));
}
private HttpWebRequest CreateRequest(string url, string method = null)
{
var httpRequest = WebRequest.Create(url) as HttpWebRequest;
ReplicateCookies(httpRequest);
httpRequest.KeepAlive = false;
httpRequest.Method = method ?? "GET";
httpRequest.Timeout = 20000;
httpRequest.Proxy = null;
httpRequest.ServicePoint.ConnectionLeaseTimeout = 20000;
httpRequest.ServicePoint.MaxIdleTime = 10000;
return httpRequest;
}
public string ReadWebPage(string url)
{
var oldCulture = Thread.CurrentThread.CurrentCulture.Name;
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-GB");
string result;
var httpRequest = CreateRequest(url);
httpRequest.Headers.Add("Accept-Language", oldCulture);
try
{
using (var httpResponse = httpRequest.GetResponse())
{
using (var stream = httpResponse.GetResponseStream())
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
reader.Close();
}
stream.Flush();
stream.Close();
}
httpResponse.Close();
}
}
finally
{
httpRequest.Abort();
}
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = new CultureInfo(oldCulture);
return result;
}
如果你想知道:
。
我已经确定:
的请求
也许值得一提的是,这是在内部MVC3 web应用程序中使用的,用于生成模板化的消息--其中模板是网页本身。应用程序像这样设置它的区域性(Global.asax.cs):
protected void Application_AcquireRequestState()
{
if (HttpContext.Current == null)
return;
if (HttpContext.Current.Session == null)
return;
if (HttpContext.Current.Session["Culture"] != null)
CultureHelper.SetCulture(HttpContext.Current.Session["Culture"].ToString());
}
帮助者类:
public static class CultureHelper
{
public static void SetCulture(string culture)
{
if (string.IsNullOrEmpty(culture))
return;
var cultureInfo = new CultureInfo(culture);
if (culture.ToLower().Equals("fr-fr") || culture.ToLower().Equals("fr"))
cultureInfo.NumberFormat.NumberGroupSeparator = cultureInfo.NumberFormat.CurrencyGroupSeparator = ".";
Thread.CurrentThread.CurrentCulture = Thread.CurrentThread.CurrentUICulture = cultureInfo;
}
}
默认情况下,法语使用空格作为数千个分隔符,原因如下--例如if语句。
ReplicateCookies方法将将会话cookie和身份验证cookie复制到内部请求,从而在产生的内部请求中保留会话/身份验证。
该站点显然具有更改当前区域性的方法(更改会话值)。在ReadWebPage方法修复之前,一切都会正常工作,直到改变了区域性。在此之后,每个HttpWebRequest.GetResponse都会超时,直到文化被更改回-在我的例子中- en-GB。
知道为什么会这样吗?我可能在某个地方做了严重的错事。就像我说的,它是在当前状态下工作的。只是不太漂亮。
发布于 2011-12-06 16:08:19
由于所有试图在任何人看到这个帖子失败之前迅速删除它,这里有一个解决方案。和往常一样,这是你从未想到的一件事--会议。当我画“砸你的头!”黑点在一张纸上,这是修正的代码:
private static void ReplicateCookies(HttpWebRequest request)
{
if (HttpContext.Current == null)
return;
if (request.CookieContainer == null)
request.CookieContainer = new CookieContainer();
foreach (var cookieKey in HttpContext.Current.Request.Cookies.Keys)
{
if (!cookieKey.ToString().Equals("ASP.NET_SessionId"))
{
var cookie = HttpContext.Current.Request.Cookies[cookieKey.ToString()];
request.CookieContainer.Add(new Cookie(cookie.Name, cookie.Value, cookie.Path, string.IsNullOrEmpty(cookie.Domain)
? HttpContext.Current.Request.Url.Host
: cookie.Domain));
}
}
}
private HttpWebRequest CreateRequest(string url, string method = null)
{
var httpRequest = WebRequest.Create(url) as HttpWebRequest;
ReplicateCookies(httpRequest);
httpRequest.KeepAlive = false;
httpRequest.Method = method ?? "GET";
httpRequest.Timeout = 20000;
httpRequest.Proxy = null;
httpRequest.ServicePoint.ConnectionLeaseTimeout = 20000;
httpRequest.ServicePoint.MaxIdleTime = 10000;
httpRequest.Headers.Add("Accept-Language", Thread.CurrentThread.CurrentCulture.Name);
return httpRequest;
}
public string ReadWebPage(string url)
{
string result;
var httpRequest = CreateRequest(url);
try
{
using (var httpResponse = httpRequest.GetResponse())
{
using (var stream = httpResponse.GetResponseStream())
{
using (var reader = new StreamReader(stream, Encoding.UTF8))
{
result = reader.ReadToEnd();
reader.Close();
}
stream.Flush();
stream.Close();
}
httpResponse.Close();
}
}
finally
{
httpRequest.Abort();
}
return result;
}
说明:问题出现在ReplicateCookies方法中。除其他外,它还重写了ASP会话cookie。由于它是在调用同一个进程,所以稍后挂起(并最终超时),因为来自初始请求的会话被锁定-而传入的请求试图使用相同的会话。
现在把这个大黑点的磁石挂在墙上.
https://stackoverflow.com/questions/8402460
复制相似问题