基础连接已经关闭 解决方案
Posted jwsoft007
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基础连接已经关闭 解决方案相关的知识,希望对你有一定的参考价值。
/// <summary>
/// iEncode=1- gb2312; 2- UTF8
/// </summary>
/// <param name="url"></param>
/// <param name="iEncode"></param>
/// <returns></returns>
public static string GetPageInfo(String url, int iEncode = 1)
WebResponse wr_result = null;
StringBuilder txthtml = new StringBuilder();
try
//WebRequest wr_req = WebRequest.Create(url);
//wr_result = wr_req.GetResponse();
HttpWebRequest wr_req = WebRequest.Create(url) as HttpWebRequest;
wr_req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 4.0) ";
//wr_req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ";
wr_req.KeepAlive = false;
wr_req.ProtocolVersion = HttpVersion.Version10;
wr_result = wr_req.GetResponse();
Stream ReceiveStream = wr_result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("gb2312");
if (iEncode == 1)
encode = System.Text.Encoding.GetEncoding("gb2312");
else if (iEncode == 2)
encode = Encoding.UTF8;
//Encoding encode = Encoding.Unicode;
//Encoding encode = Encoding.UTF8;
StreamReader sr = new StreamReader(ReceiveStream, encode);
if (true)
Char[] read = new Char[256];
int count = sr.Read(read, 0, 256);
while (count > 0)
String str = new String(read, 0, count);
txthtml.Append(str);
count = sr.Read(read, 0, 256);
catch (Exception ex)
txthtml.Append("err");
finally
if (wr_result != null)
wr_result.Close();
return txthtml.ToString();
用这2行
WebRequest wr_req = WebRequest.Create(url);
wr_result = wr_req.GetResponse();
就会出现 基础连接已经关闭的错误
改用
HttpWebRequest wr_req = WebRequest.Create(url) as HttpWebRequest;
wr_req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 4.0) ";
//wr_req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727) ";
wr_req.KeepAlive = false;
wr_req.ProtocolVersion = HttpVersion.Version10;
wr_result = wr_req.GetResponse();
就可以了.
以上是关于基础连接已经关闭 解决方案的主要内容,如果未能解决你的问题,请参考以下文章
httpWebRequest请求错误,基础连接已经关闭: 连接被意外关闭
解决“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系“的问题
Entity Framework4.3安装以及错误(基础连接已经关闭:未能为SSL/TLS……)问题解决!