首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WebRequest极慢

WebRequest极慢
EN

Stack Overflow用户
提问于 2013-03-14 12:03:45
回答 1查看 649关注 0票数 1

我的方法如下所示:

代码语言:javascript
复制
public string Request(string action, NameValueCollection parameters, uint? timeoutInSeconds = null)
{
    parameters = parameters ?? new NameValueCollection();
    ProvideCredentialsFor(ref parameters);

    var data = parameters.ToUrlParams(); // my extension method converts the collection to a string, works well

    byte[] dataStream = Encoding.UTF8.GetBytes(data);
    string request = ServiceUrl + action;
    var webRequest = (HttpWebRequest)WebRequest.Create(request);
    webRequest.AllowAutoRedirect = false;
    webRequest.Method = "POST";
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.ContentLength = dataStream.Length;
    webRequest.Timeout = (int)(timeoutInSeconds == null ? DefaultTimeoutMs : timeoutInSeconds * 1000);
    webRequest.Proxy = null; // should make it faster...

    using (var newStream = webRequest.GetRequestStream())
    {
        newStream.Write(dataStream, 0, dataStream.Length);
    }
    var webResponse = (HttpWebResponse)webRequest.GetResponse();

    string uri = webResponse.Headers["Location"];

    string result;
    using (var sr = new StreamReader(webResponse.GetResponseStream()))
    {
        result = sr.ReadToEnd();
    }


    return result;
}

服务器发送JSON作为响应。对于小型JSON,它可以正常工作,但是当我请求一个大型JSON时,会出现问题。总的来说,我指的是在浏览器中出现需要1-2分钟的东西(google,包括服务器端的生成时间)。它实际上是412 of的文本。当我试图使用上面的方法请求相同的JSON时,我会得到一个web异常(timeout)。我将超时更改为10分钟(至少比铬长5倍)。还是一样的。

有什么想法吗?

编辑

这似乎与MS技术有关。在IE上,这个JSON也不会加载。

EN

回答 1

Stack Overflow用户

发布于 2013-08-12 08:44:07

确保您关闭了请求。否则,一旦达到允许连接的最大数量(对于我来说,只有4个),您必须等待前面的连接超时。最好的方法是

代码语言:javascript
复制
using (var response = webRequest.GetResponse()) {...
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15408776

复制
相关文章

相似问题

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