HTTPWebResponse和StreamReader需要两倍的时间

本文关键字:两倍 时间 StreamReader HTTPWebResponse | 更新日期: 2023-09-27 18:24:14

使用ajax发出请求,在Internet Explorer 上需要13秒

var start = new Date().getTime();            
var xmlRequest = ""; //XML REQUEST
var xmlHttp=null
try  { xmlHttp=new XMLHttpRequest(); }
catch (e)
{
       try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
       catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }
}
var sURL = ""; //DESTINATION URL
xmlHttp.open("POST", sURL, false);
xmlHttp.setRequestHeader("Content-Type","text/xml");
xmlHttp.send(xmlRequest);
var end = new Date().getTime();
var time = end - start;
alert('Execution time: ' + time); // 13 seconds
alert("Response:'n" + xmlHttp.responseText); //XML RESPONSE
xmlHttp.close();

现在,尝试服务器端:

public string MakeWebRequest(string url, string xmlRequest)
{
    try
    {
        string responseContent;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        byte[] byteArray = Encoding.UTF8.GetBytes(xmlRequest);
        request.ContentType = "text/xml";
        request.ContentLength = byteArray.Length;
        //request.Proxy = null;
        //request.Proxy = WebRequest.GetSystemWebProxy();
        request.Proxy = System.Net.WebRequest.DefaultWebProxy;
        using (Stream requestStream = request.GetRequestStream())
        {
            requestStream.Write(byteArray, 0, byteArray.Length);
        }        
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream responseStream = response.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(responseStream))
                    responseContent = sr.ReadToEnd();
            }
        }
        return responseContent;
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

相同的URL和相同的xml请求,需要40秒才能响应,为什么??

与结果相同

request.Proxy = null;

和:

request.Proxy = WebRequest.GetSystemWebProxy();

和:

request.Proxy = System.Net.WebRequest.DefaultWebProxy;

和:

<system.net>
  <defaultProxy enabled="false">
    <proxy/>
    <bypasslist/>
    <module/>
  </defaultProxy>
</system.net>

和:

<system.net>
  <connectionManagement>
     <add address="*" maxconnection="65000" />
  </connectionManagement>
</system.net>

谢谢你的帮助!

HTTPWebResponse和StreamReader需要两倍的时间

OMFG!我终于找到了解决方案!我使用wireshark来查看请求标头。HttpWebRequest默认添加"Expect:100-continue"。我必须删除参数才能工作!

request.ServicePoint.Expect100Continue = false;