.net SOCKET发送请求获取不到数据

bichir 2013-12-09 04:45:30

namespace SocketForm
{
public class HttpServer
{
private static List<Socket> socket = new List<Socket>();//存闲置SOCKET
private Socket currentSocket;//当前正在用的SOCKET
private String url;//地址
private List<String> cookie;//cookie
private List<String> param;//参数
private String method;//方式
public HttpServer()
{
if (socket.Count > 0)
{
currentSocket = socket[0];
socket.RemoveAt(0);//从池中删掉
}
else
{
currentSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

}
}

/***
*
* url 地址
* param 参数
*
***/
public void get(String url,List<String> param,List<String> cookie)
{
this.url = url;
this.param = param;
this.cookie = cookie;
this.method = "get";
String address = conect();//建立建连并分离地址
//HTTP头
StringBuilder sb = new StringBuilder();
sb.Append("GET ").Append(address).Append(" HTTP/1.1\r\n");
sb.Append("Host: www.baidu.com\r\n");
sb.Append("Accept: application/x-ms-application, image/jpeg, application/xaml+xml, image/gif, image/pjpeg, application/x-ms-xbap, */*\r\n");
sb.Append("Accept-Encoding: gzip, deflate\r\n");
sb.Append("Accept-Language: zh-CN\r\n");
sb.Append("Content-Type: application/x-www-form-urlencoded\r\n");
sb.Append("UA-CPU: AMD64\r\n");
sb.Append("User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)\r\n");
if (cookie != null)
{
foreach (String c in cookie)
{
sb.Append("Cookie: ").Append(c).Append("\r\n");
}
}
sb.Append("Connection: Keep-Alive\r\n");
this.currentSocket.Send(Encoding.UTF8.GetBytes(sb.ToString()));
}
/**
* 最终发送涵数并接收消息
* coun接收到的字节长度
**/
public List<byte[]> send(out long count)
{
count = 0L;
List<byte[]> b = new List<byte[]>();
this.currentSocket.Send(Encoding.UTF8.GetBytes("/r/n"));//发送请求头结否标记
//接收消息
int bytes;
do{//**********这里获取不到数据,执行到这里会阻塞几秒,最后返回0******8//////
byte[] recvBytes = new byte[1024];
bytes = this.currentSocket.Receive(recvBytes, recvBytes.Length, 0);//从服务器端接受返回信息
b.Add(recvBytes);
count += bytes;
Thread.Sleep(100);
} while (bytes!=0);
return b;
}

/**
* 检查是否已建立SOCKET连接
**/
private String conect()
{
String host = this.url.Substring(0, this.url.IndexOf("/") < 0 ? this.url.Length : this.url.IndexOf("/"));
if (this.currentSocket.RemoteEndPoint == null)
{
this.currentSocket.Connect(Dns.GetHostAddresses("www.baidu.com"), 80);
}
return this.url.Substring(host.Length).Length>0?this.url.Substring(host.Length):"/";
}
}
}


下面是调用代码


HttpServer s = new HttpServer();
s.get("www.baidu.com",null,null);
long count;
List<byte[]> b = s.send(out count);
for (int i = 0; i < b.Count; i++)
{
count -= b[i].Length;
if (count > 0)
{
System.Console.WriteLine(Encoding.Default.GetString(b[i], 0, b[i].Length));
}
else
{
System.Console.WriteLine(Encoding.Default.GetString(b[i], 0,(int)(b[i].Length-count)));
}
}
...全文
166 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
bichir 2013-12-09
  • 打赏
  • 举报
回复
引用 1 楼 cheery_an 的回复:
先mark。
什么意思

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧