c# 通过socket模拟HTTP协议,解析HTTP包头
2008-03-18 18:41
495 查看
找到很好的文章,可以应用到我正在开发的彩信MM7协议中
C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#
**//// <summary>
/// 接收数据的方法
/// </summary>
/// <param name="socket">Socket连接</param>
/// <param name="size">要接收的数据长度</param>
/// <returns>返回收到的字节数组</returns>
public static byte[] ReceiveData(Socket socket,int size)
{
int total=0; //收到的总的字节数
int dataleft=size; //剩余的字节数
byte[] data=new byte[size]; //接收数据的数组
int rece=0; //收到的字节数
//循环接收数据
while(total<size)
{
rece=socket.Receive(data,total,dataleft,SocketFlags.None);
//如果收到的字节数为0,那么说明连接断开,返回空的字节数组
if(rece==0)
{
break;
}
total+=rece; //收到的字节数长度++
dataleft-=rece; //剩余的字节数--
}
return data; //返回
}
c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#
方法重载
public static byte[] ReceiveData(Socket socket)
{
StringBuilder header=new StringBuilder();
string headertext="";
while(true)
{
byte[] data=new byte[1];
//接收一个字节
int rece=socket.Receive(data,1,SocketFlags.None);
//转换为char
char c=(char)data[0];
header.Append(c);
//检查是否到了包头末尾,如果到了包头末尾,那么停止
//读取
if(header.ToString().IndexOf("/r/n/r/n")>0)
{
string content="CONTENT-LENGTH:";
int start=header.ToString().ToUpper().IndexOf(content);
headertext=header.ToString().Substring(start+content.Length);
int end=headertext.IndexOf("/r/n");
headertext=headertext.Substring(0,end); //包体长度
break;
}
}
//
byte[] ds=ReceiveData(socket,Convert.ToInt16(headertext));
return ds;
}
C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#C#
**//// <summary>
/// 接收数据的方法
/// </summary>
/// <param name="socket">Socket连接</param>
/// <param name="size">要接收的数据长度</param>
/// <returns>返回收到的字节数组</returns>
public static byte[] ReceiveData(Socket socket,int size)
{
int total=0; //收到的总的字节数
int dataleft=size; //剩余的字节数
byte[] data=new byte[size]; //接收数据的数组
int rece=0; //收到的字节数
//循环接收数据
while(total<size)
{
rece=socket.Receive(data,total,dataleft,SocketFlags.None);
//如果收到的字节数为0,那么说明连接断开,返回空的字节数组
if(rece==0)
{
break;
}
total+=rece; //收到的字节数长度++
dataleft-=rece; //剩余的字节数--
}
return data; //返回
}
c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#c#
方法重载
public static byte[] ReceiveData(Socket socket)
{
StringBuilder header=new StringBuilder();
string headertext="";
while(true)
{
byte[] data=new byte[1];
//接收一个字节
int rece=socket.Receive(data,1,SocketFlags.None);
//转换为char
char c=(char)data[0];
header.Append(c);
//检查是否到了包头末尾,如果到了包头末尾,那么停止
//读取
if(header.ToString().IndexOf("/r/n/r/n")>0)
{
string content="CONTENT-LENGTH:";
int start=header.ToString().ToUpper().IndexOf(content);
headertext=header.ToString().Substring(start+content.Length);
int end=headertext.IndexOf("/r/n");
headertext=headertext.Substring(0,end); //包体长度
break;
}
}
//
byte[] ds=ReceiveData(socket,Convert.ToInt16(headertext));
return ds;
}
相关文章推荐
- C#如何通过SOCKET的方式获取HTTPONLY COOKIE
- C# socket 解析http 协议
- C#如何通过Socket的方式获取httponly cookie
- Socket模拟Http协议,读取http header,根据content-length的值,读取Http body
- 一下内容纯属转载(C#模拟http登陆)
- PHP模拟发送POST请求之一、HTTP协议头部解析
- 网站建设底层知识Socket与Http解析
- 转:使用C#的HttpWebRequest模拟登陆网站
- 服务调用者发送soap消息通过http发送服务器的过程解析
- C# 通过socket实现UDP 通信
- 使用C#发送Http 请求实现模拟登陆(以博客园为例)
- C#模拟http 发送post或get请求
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- httpClient4.3 模拟Post提交,模拟Post上传文件并解决服务端使用spingMVC时upload.parseRequest(request)解析不到File,Pos简单抓取页面数据
- 通过C#的HttpClient模拟form表单请求
- C# HttpWebRequest\HttpWebResponse\WebClient发送请求解析json数据
- 利用C#进行socket编程(模拟服务器和客户端通信)
- C# : WebRequest发起Http Post请求模拟登陆并cookie处理示例
- 网站建设底层知识Socket与Http解析
- c# 之HttpContext通过表单提交后批量转换为Model