首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如果更改了HttpWebRequest,Thread.CurrentThread.CurrentCulture就会超时

如果更改了HttpWebRequest,Thread.CurrentThread.CurrentCulture就会超时
EN

Stack Overflow用户
提问于 2011-12-06 15:32:57
回答 1查看 1.4K关注 0票数 1

如果您将Thread.CurrentThread.CurrentCulture更改为与安装(OS区域性)不同的内容,则对HttpWebRequest.GetResponse()的调用将始终超时。

为了澄清这一点,下面是我的实用工具类的内容,它正在进行肮脏的工作:

代码语言:javascript
运行
复制
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;
}

如果你想知道:

  • in ReplicateCookies,如果以null作为域调用CookieCOntainer.Add,则它在运行时
  • 中失败--代码已经包含了一个解决CurrentCulture问题的修补程序。继续,取出所有正在更改区域性的代码,将您的线程区域性设置为某些内容(例如"fr-FR"),并让代码转一下。

我已经确定:

  • no代理正在干扰请求
  • 没有防火墙设置可以阻止类似

的请求

也许值得一提的是,这是在内部MVC3 web应用程序中使用的,用于生成模板化的消息--其中模板是网页本身。应用程序像这样设置它的区域性(Global.asax.cs):

代码语言:javascript
运行
复制
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());
}

帮助者类:

代码语言:javascript
运行
复制
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。

知道为什么会这样吗?我可能在某个地方做了严重的错事。就像我说的,它是在当前状态下工作的。只是不太漂亮。

EN

回答 1

Stack Overflow用户

发布于 2011-12-06 16:08:19

由于所有试图在任何人看到这个帖子失败之前迅速删除它,这里有一个解决方案。和往常一样,这是你从未想到的一件事--会议。当我画“砸你的头!”黑点在一张纸上,这是修正的代码:

代码语言:javascript
运行
复制
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。由于它是在调用同一个进程,所以稍后挂起(并最终超时),因为来自初始请求的会话被锁定-而传入的请求试图使用相同的会话。

现在把这个大黑点的磁石挂在墙上.

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8402460

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档