HTTP在C#插座 [英] HTTP over C# sockets
本文介绍了HTTP在C#插座的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图发送HTTP请求,并从该服务器在C#插座收到的反响,我用新的这种语言。
I am trying to send HTTP request and recieve responce from the server over C# sockets, and i'm new with this language.
我已经写了下面的代码(IP正确解析):
I've wrote following code (IP resolved correctly):
IPEndPoint RHost = new IPEndPoint(IP, Port);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(RHost);
String HTTPRequestHeaders_String = "GET ?q=fdgdfg HTTP/1.0
Host: google.com
Keep-Alive: 300
Connection: Keep-Alive
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.205 Safari/534.16
Referer: http://google.com/";
MessageBox.Show(HTTPRequestHeaders_String, "Request");
byte[] HTTPRequestHeaders = System.Text.Encoding.ASCII.GetBytes(HTTPRequestHeaders_String);
socket.Send(HTTPRequestHeaders, SocketFlags.None);
String Response = "";
byte[] buffer = new byte[(int) socket.ReceiveBufferSize];
int bytes;
do
{
// On this lane program stops to react
bytes = socket.Receive(buffer);
// This line cannot be reached, tested with breakpoint
Response += Encoding.ASCII.GetString(buffer, 0, bytes);
}
while (bytes >= 0);
MessageBox.Show(Response, "Response");
我究竟做错了什么?我只需要加载页面的完整HTML,或响应至少几个字符(我不能做,即使这一点)。
What am i doing wrong? I need just to load full HTML of page, or at least few characters from response (i cant do even this).
推荐答案
// WWW:如果你想这样做原材料, HTTP我会建议寻找到协议本身。 w3.org/Protocols/HTTP/1.0/spec.html#Request
I would suggest looking into the protocol itself if you want to do this raw, http://www.w3.org/Protocols/HTTP/1.0/spec.html#Request
和尝试发送CRLF终止请求;)
And try sending the CRLF to terminate the request ;)
这篇关于HTTP在C#插座的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文