基于 C# 套接字的 HTTP

发布于 2024-12-10 13:26:37 字数 1164 浏览 0 评论 0原文

我正在尝试通过 C# 套接字发送 HTTP 请求并从服务器接收响应,而且我是这种语言的新手。

我写了以下代码(IP 解析正确):

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,或者至少加载响应中的几个字符(我什至不能这样做)。

I am trying to send HTTP request and recieve responce from the server over C# sockets, and i'm new with this language.

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");

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

脸赞 2024-12-17 13:26:37

如果您想原始执行此操作,我建议您查看协议本身, http ://www.w3.org/Protocols/HTTP/1.0/spec.html#Request

并尝试发送 CRLF 来终止请求;)

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

And try sending the CRLF to terminate the request ;)

赠意 2024-12-17 13:26:37
var webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream responseStream = webClient.OpenRead("http://www.google.com");
if (responseStream != null)
{
   var responseReader = new StreamReader(responseStream);
   string response = responseReader.ReadToEnd();
   MessageBox.Show(response);
}
var webClient = new WebClient();
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream responseStream = webClient.OpenRead("http://www.google.com");
if (responseStream != null)
{
   var responseReader = new StreamReader(responseStream);
   string response = responseReader.ReadToEnd();
   MessageBox.Show(response);
}
话少心凉 2024-12-17 13:26:37

我发现 Mentalis 代理对于理解套接字级别的 Http 请求/响应周期非常有帮助: http://www.mentalis.org/soft/projects/proxy/

I found the Mentalis Proxy to be extremely helpful in understanding the Http Request/Response cycle at a socket level: http://www.mentalis.org/soft/projects/proxy/

陪你搞怪i 2024-12-17 13:26:37

TcpClient 类,一方面允许您完全控制请求(您将请求正文创建为字符串),另一方面比低级套接字使用起来简单得多。

http://msdn.microsoft.com/en-us /library/system.net.sockets.tcpclient.aspx

There is the TcpClient class which on one hand allows you to have a full control over the request (you create request body as string) and on the other hand is much simpler to use than a low-level socket.

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文