HttpWebRequest的常见错误使用TcpClient可避免
网络编程 2021-07-04 22:40www.168986.cn编程入门
有时使用HttpWebRequest对象会出现错误有三种服务器提交了协议冲突/基础连接已经关闭:连接被意外关闭/无法发送具有此谓词类型的内容正文,感兴趣的朋友可以参考下本文
有时使用HttpWebRequest对象会出现错误,有三种
1、System.Net.WebException: 服务器提交了协议冲突. Section=ResponseStatusLine
2、System.Net.WebException: 基础连接已经关闭: 连接被意外关闭。
3、System.Net.ProtocolViolationException: 无法发送具有此谓词类型的内容正文。
使用TcpClient对象搞定
private string GetHTMLTCP(string URL)
{
string strHTML = "";//用来保存获得的HTML代码
TcpClient clientSocket = new TcpClient();
Uri URI = new Uri(URL);
clientSocket.Connect(URI.Host, URI.Port);
StringBuilder RequestHeaders = new StringBuilder();//用来保存HTML协议头部信息
RequestHeaders.AppendFormat("{0} {1} HTTP/1.1\r\n", "GET", URI.PathAndQuery);
RequestHeaders.AppendFormat("Connection:close\r\n");
RequestHeaders.AppendFormat("Host:{0}\r\n", URI.Host);
RequestHeaders.AppendFormat("Aept:/\r\n");
RequestHeaders.AppendFormat("Aept-Language:zh-\r\n");
RequestHeaders.AppendFormat("User-Agent:Mozilla/4.0 (patible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\n\r\n");
Encoding encoding = Encoding.Default;
byte[] request = encoding.GetBytes(RequestHeaders.ToString());
clientSocket.Client.Send(request);
//获取要保存的网络流
Stream readStream = clientSocket.GetStream();
StreamReader sr = new StreamReader(readStream, Encoding.Default);
strHTML = sr.ReadToEnd();
readStream.Close();
clientSocket.Close();
return strHTML;
}
使用 HttpWebRequest请求页面
///
/// 获取html源码
///
///
///
///
static string GetHTML(string url, string param)
{
try
{
Uri uri = new Uri(url);
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
myReq.Headers.Add("Aept-Encoding", "gzip,deflate");//sdch
byte[] byData = Encoding.Default.GetBytes(param);
myReq.Method = "post";
myReq.ContentLength = byData.Length;
Stream reqStrem = myReq.GetRequestStream();
reqStrem.Write(byData, 0, byData.Length);
reqStrem.Close();
HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
Stream recStream = result.GetResponseStream();
//如果是Gzip方式则需要解压
recStream = new GZipStream(recStream, CompressionMode.Depress);
StreamReader redStream = new StreamReader(recStream, System.Text.Encoding.Default);
string strHTML = redStream.ReadToEnd();
redStream.Close();
recStream.Close();
result.Close();
return strHTML;
}
catch (Exception)
{
return "";
}
}
1、System.Net.WebException: 服务器提交了协议冲突. Section=ResponseStatusLine
2、System.Net.WebException: 基础连接已经关闭: 连接被意外关闭。
3、System.Net.ProtocolViolationException: 无法发送具有此谓词类型的内容正文。
使用TcpClient对象搞定
代码如下:
private string GetHTMLTCP(string URL)
{
string strHTML = "";//用来保存获得的HTML代码
TcpClient clientSocket = new TcpClient();
Uri URI = new Uri(URL);
clientSocket.Connect(URI.Host, URI.Port);
StringBuilder RequestHeaders = new StringBuilder();//用来保存HTML协议头部信息
RequestHeaders.AppendFormat("{0} {1} HTTP/1.1\r\n", "GET", URI.PathAndQuery);
RequestHeaders.AppendFormat("Connection:close\r\n");
RequestHeaders.AppendFormat("Host:{0}\r\n", URI.Host);
RequestHeaders.AppendFormat("Aept:/\r\n");
RequestHeaders.AppendFormat("Aept-Language:zh-\r\n");
RequestHeaders.AppendFormat("User-Agent:Mozilla/4.0 (patible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\n\r\n");
Encoding encoding = Encoding.Default;
byte[] request = encoding.GetBytes(RequestHeaders.ToString());
clientSocket.Client.Send(request);
//获取要保存的网络流
Stream readStream = clientSocket.GetStream();
StreamReader sr = new StreamReader(readStream, Encoding.Default);
strHTML = sr.ReadToEnd();
readStream.Close();
clientSocket.Close();
return strHTML;
}
使用 HttpWebRequest请求页面
代码如下:
///
/// 获取html源码
///
///
///
///
static string GetHTML(string url, string param)
{
try
{
Uri uri = new Uri(url);
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
myReq.Headers.Add("Aept-Encoding", "gzip,deflate");//sdch
byte[] byData = Encoding.Default.GetBytes(param);
myReq.Method = "post";
myReq.ContentLength = byData.Length;
Stream reqStrem = myReq.GetRequestStream();
reqStrem.Write(byData, 0, byData.Length);
reqStrem.Close();
HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
Stream recStream = result.GetResponseStream();
//如果是Gzip方式则需要解压
recStream = new GZipStream(recStream, CompressionMode.Depress);
StreamReader redStream = new StreamReader(recStream, System.Text.Encoding.Default);
string strHTML = redStream.ReadToEnd();
redStream.Close();
recStream.Close();
result.Close();
return strHTML;
}
catch (Exception)
{
return "";
}
}
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程