我使用此代码编写用作本地 http 请求服务器的 Windows 服务。
public void StartMe()
{
System.Net.IPAddress localAddr = System.Net.IPAddress.Parse("127.0.0.1");
System.Net.Sockets.TcpListener server = new System.Net.Sockets.TcpListener(localAddr, 1234);
server.Start();
Byte[] bytes = new Byte[1024];
String data = null;
while (RunThread)
{
System.Net.Sockets.TcpClient client = server.AcceptTcpClient();
data = null;
System.Net.Sockets.NetworkStream stream = client.GetStream();
stream.Read(bytes, 0, bytes.Length);
data = System.Text.Encoding.ASCII.GetString(bytes);
System.IO.StreamWriter sw = new System.IO.StreamWriter("c:\\MyLog.txt", true);
sw.WriteLine(data);
sw.Close();
client.Close();
}
}
我对这段代码有一些问题:首先在data
字符串中,我在浏览器中写了这个 URL 后得到了这样的东西http://127.0.0.1:1234/helloWorld
GET /helloWorld HTTP/1.1
Host: 127.0.0.1:1234
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
Accept-Encoding: gzip,deflate,sdch
Accept-Language: he-IL,he;q=0.8,en-US;q=0.6,en;q=0.4
Accept-Charset: windows-1255,utf-8;q=0.7,*;q=0.3
我想知道我怎样才能helloWorld
从这个例子中得到唯一的。第二个问题是我希望服务器会对浏览器做出响应,它只让我关闭连接。