HTTPWebResponse和StreamReader需要两倍的时间

时间:2014-09-23 12:55:12

标签: c# asp.net .net streamreader httpwebresponse

使用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>

寻求任何帮助!

1 个答案:

答案 0 :(得分:0)

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

request.ServicePoint.Expect100Continue = false;