HTTP连接错误

时间:2017-07-03 09:10:01

标签: c# http

收到此错误基础连接已关闭:发送时发生意外错误。

以下是c#代码的一部分

        StringBuilder postData = new StringBuilder();
        postData.Append("cmpi_msg=" + 
        HttpUtility.UrlEncode(elem.ToString()));

        ASCIIEncoding encoding = new ASCIIEncoding();
        byte[] data = encoding.GetBytes(postData.ToString());
       // ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.Timeout = 30 * 1000;
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = data.Length;
        Stream newStream = request.GetRequestStream();
        newStream.Write(data, 0, data.Length);
        //newStream.Flush();
        //newStream.Close();
        //request.KeepAlive = false;

        string resText = "";
        //ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            Encoding enc = System.Text.Encoding.GetEncoding(1252);
            using (StreamReader rs = new StreamReader(response.GetResponseStream(), enc))
            {
                resText = rs.ReadToEnd();
                //Console.WriteLine("RES:" + resText);
            }
        }

0 个答案:

没有答案