关闭

C# 使用TcpClient模拟HTTP请求的使用

发表于:2013-10-22 10:19

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:geraldjing    来源:51Testing软件测试网采编

  一般做http模拟请求的一般使用HttpWebRequest这种方式,但这种方式也存在着一些bug,因为HttpWebRequest是使用的Windows系统定制的net通信,xp跟win7他们有的地方就不一样,HttpWebRequest可以说是给Windows定制的模拟操作,如果遇见某些服务器,可能根本不吃这一套,就像我最近遇到的问题,设置的Content-Length竟然可能视为无效,但正常访问抓包ok,程序跑起来有一半多时候会出错,还有其他的一些问题,比如设置了进制自动重定向,如果再设置Expect100Continue时候,前面的设置会发生无效,无奈,尝试使用TcpClient方式解决了。当下载文件时候,HttpWebRequest好像根据操作系统有最大文件的限制,所以这个时候使用TcpClient也是不错的选择
  下面是我下好的一个类,我使用已经够了,可能中间有不足,希望大家指出
class HTTPClass
{
public Response HTTP(string _url, string _type, string _postdata, string _cookie, Encoding _responseEncode)
{
try
{
TcpClient clientSocket = new TcpClient();
Uri URI = new Uri(_url);
clientSocket.Connect(URI.Host, URI.Port);
StringBuilder RequestHeaders = new StringBuilder();
RequestHeaders.Append(_type + " " + URI.PathAndQuery + " HTTP/1.1\r\n");
RequestHeaders.Append("Content-Type:application/x-www-form-urlencoded\r\n");
RequestHeaders.Append("User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11\r\n");
RequestHeaders.Append("Cookie:" + _cookie + "\r\n");
RequestHeaders.Append("Accept:*/*\r\n");
RequestHeaders.Append("Host:" + URI.Host + "\r\n");
RequestHeaders.Append("Content-Length:" + _postdata.Length + "\r\n");
RequestHeaders.Append("Connection:close\r\n\r\n");
byte[] request = Encoding.UTF8.GetBytes(RequestHeaders.ToString() + _postdata);
clientSocket.Client.Send(request);
byte[] responseByte = new byte[1024000];
int len = clientSocket.Client.Receive(responseByte);
string result = Encoding.UTF8.GetString(responseByte, 0, len);
clientSocket.Close();
int headerIndex = result.IndexOf("\r\n\r\n");
string[] headerStr = result.Substring(0, headerIndex).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
Dictionary responseHeader = new Dictionary();
for (int i = 0; i < headerStr.Length; i++)
{
string[] temp = headerStr[i].Split(new string[] { ": " }, StringSplitOptions.RemoveEmptyEntries);
if (temp.Length == 2)
{
if (responseHeader.ContainsKey(temp[0]))
{
responseHeader[temp[0]] = temp[1];
}
else
{
responseHeader.Add(temp[0], temp[1]);
}
}
}
Response response = new Response();
response.HTTPResponseHeader = responseHeader;
string[] httpstatus = headerStr[0].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
if (httpstatus.Length > 2)
{
response.HTTPStatusCode = httpstatus[1];
}
else
{
response.HTTPStatusCode = "400";
}
response.HTTPResponseText = _responseEncode.GetString(Encoding.UTF8.GetBytes(result.Substring(headerIndex + 4)));
return response;
}
catch
{
return null;
}
}
}
class Response
{
string hTTPStatusCode;
///
/// http状态代码
///
public string HTTPStatusCode
{
get { return hTTPStatusCode; }
set { hTTPStatusCode = value; }
}
Dictionary hTTPResponseHeader;
///
/// Response的header
///
public Dictionary HTTPResponseHeader
{
get { return hTTPResponseHeader; }
set { hTTPResponseHeader = value; }
}
string hTTPResponseText;
///
/// html代码
///
public string HTTPResponseText
{
get { return hTTPResponseText; }
set { hTTPResponseText = value; }
}
}
  只给大家做参考使用,具体情况具体分析
  以上程序对对于来获取一个小网页完全可以,但如果你网页比较大的话,就会出现接收不全的问题,这个问题主要是因为服务器那边响应请求的时候,首先把要响应的内容写如缓冲区,然后根据本身的设定一次次发送内容,而我这只是接受了一次,所以当文本过大时候,就会只接收一部分,如果你的内容过大,可以循环接收下来解决这个问题,当然,可能你有更好的方法
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号