TCP模拟HTTP协议,无法发送请求命令
c#里面TCP协议, socket异步传输数据。只要发送的字符串由“GET”或者“HTTP”等http协议里的请求命令打头,数据就发不出去,用了嗅探器抓包都抓不到。而同样的代码,只要把打头的字符改掉,改成小写也好,加个空格也好,数据就能发送过去,但是服务器就不响应。请问这是什么问题啊。
部分代码如下
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
public void ServerConnected(IAsyncResult ar)
{
Socket CLientSocket = (Socket)ar.AsyncState;
try
{
CLientSocket.EndConnect(ar);
DataEventArgs dataEvent = new DataEventArgs(null, data);
OnConnectEvent(dataEvent);//触发建立连接事件
CLientSocket.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(ReceiveData), CLientSocket);
string buffer;
buffer = "GET / HTTP/1.1\r\n ";
buffer += "Accept: */*\r\n ";
buffer += "User-Agent: NSPlayer/9.0.0.3265 WMFSDK/9.0\r\n ";
buffer += "Accept-Encoding: gzip, deflate\r\n ";
buffer += "Host: 192.168.1.8:8080\r\n ";
buffer += "Connection: Keep-Alive\r\n ";
byte [] senddata = System.Text.Encoding.ASCII .GetBytes(buffer);