编写" RAW"在C#中的HTTP客户端 [英] Writing a "raw" HTTP client in C#
问题描述
我试图写在C#中的原始HTTP客户端。你可能会问,为什么?
I am trying to write a "raw" HTTP client in C#. You may ask why??
我的目标是实现在J2ME中的HTTP客户端(只能做GET和有限POST),但首先我需要了解HTTP协议更好的(因此C# 。尝试)
My aim is to implement an HTTP client in J2ME (which can only do GET and limited POST), but first I need to understand the HTTP protocol better (hence the C# attempt).
我的第一次尝试是失败的:
My first attempts are failing:
var requestBytes = Encoding.UTF8.GetBytes(@"GET / HTTP/1.1
User-Agent: CSharp
Host: www.google.com
");
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect("www.google.com", 80);
socket.Send(requestBytes);
var responseBytes = new byte[socket.ReceiveBufferSize];
socket.Receive(responseBytes);
Console.Out.Write(Encoding.UTF8.GetString(responseBytes));
插座打开,但随后在接收呼叫块。几秒钟后,远程主机关闭连接。
The socket opens, but then blocks at the Receive call. After a couple of seconds the remote host closes the connection.
任何想法?
同样的发生在我请试着用腻子RAW模式进行连接。
The same happens when I try to connect using the RAW mode in puTTY.
推荐答案
这可能,如果你正在测试这个东西,安装IIS最好或Apache在本地,然后使用地址127.0.0.1;它给你更多的余地来测试不同的事情!
It might be best if you're testing this thing, to install IIS or Apache locally and then use the address 127.0.0.1; it gives you more scope to test different things!
作为一个有点放不开,我不喜欢它,如果有人用我的网站,以测试其执行的HTTP协议。
Being a bit of a prude, I wouldn't like it if somebody used my website to test their implementation of the HTTP protocol.
这篇关于编写" RAW"在C#中的HTTP客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!