C#HttpWebRequest请求连接关闭

时间:2015-07-28 00:58:02

标签: c# http connection httprequest

为什么还要等待Httprequest的回复?如何关闭连接?永远得到100-继续!

    var request = (HttpWebRequest)WebRequest.Create("http://192.168.0.193/login.aspx");

    var postData = "Login=A@Pass=B";
    var data = Encoding.ASCII.GetBytes(postData);
    request.Method = "POST"; //01
    request.KeepAlive = false;
    //request.Connection ="Close"; 
    request.ProtocolVersion = HttpVersion.Version10; //02
    request.Host = "192.168.0.193"; //03
    request.UserAgent = "Mozilla/4.0"; //04
    request.ContentType = "application/x-www-form-urlencoded"; //05
    request.ContentLength = data.Length; //05
    request.Headers.Add("Pragma", "no-cache"); //06
    request.Accept = "*/*"; //07



    using (var stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
        stream.Close();              
    }

    request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);            

    var response = (HttpWebResponse)request.GetResponse();

    var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
}

请告知。

2 个答案:

答案 0 :(得分:0)

你试过这个吗?

request.ServicePoint.Expect100Continue = false;

答案 1 :(得分:0)

您正在等待,因为“login.aspx”未发送回HttpResponse。由于没有返回HttpResponse,请求将超时。如果要获取http响应,则必须使用SOAP或REST协议编写Web服务。 Web服务可以返回一个HttpResponse,然后您可以使用WebRequest捕获该问题。