【发布时间】:2011-02-25 15:19:50
【问题描述】:
我正在尝试使用 UDP 发送 HTTP GET 请求(因为来自侦听服务器的回复无关紧要,我不想阻止程序)
这是代码:
System.Net.Sockets.UdpClient client = new System.Net.Sockets.UdpClient();
client.Connect("www.domainname.com", 80);
string request_header = "GET /ping.php HTTP/1.1\r\nHost: www.domainname.com\r\n\r\n";
byte[] stre = System.Text.Encoding.ASCII.GetBytes(request_header);
client.Send(stre, stre.Length);
System.Net.IPEndPoint RemoteIpEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
byte[] receiveBytes = client.Receive(ref RemoteIpEndPoint);
string returnData = System.Text.Encoding.ASCII.GetString(receiveBytes);
client.Close();
首先,服务器似乎没有收到请求,所以我在想发送它时可能出了什么问题? 其次,程序挂在 client.Receive(ref RemoteIpEndPoint) 上,并在那里等待。似乎没有收到任何数据。
我已经尝试过改变...
System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
到...
System.Net.IPEndPoint(System.Net.IPAddress.Any, 80);
...但没有运气。
【问题讨论】:
-
HTTP over UDP,真的吗?服务器是否甚至在端口 80 上侦听 UDP 请求?我对此表示怀疑。只需使用 TCP 就可以了。
-
好的,那么 UDP 可能是错误的方式吗?我真正想要的是发送 GET 请求的最少进程消耗方式(不阻塞发送客户端)
标签: c# asp.net http sockets udp