I’m trying to send a HTTP GET request with UDP (since the reply from the listening server is irrelevant and I don’t want to block the program)
This is the code:
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();
First, the request doesn’t seem to be receieved at the server, so I’m thinking perhaps something goes wrong when sending it?
Second, the program hangs on client.Receive(ref RemoteIpEndPoint), and just waits there. No data seems to be received.
I have tried to change…
System.Net.IPEndPoint(System.Net.IPAddress.Any, 0);
to…
System.Net.IPEndPoint(System.Net.IPAddress.Any, 80);
…but with no luck.
If you don’t want to block the client then use the async socket methods, but use TCP. I doubt you’ll find a web server that listens on UDP for HTTP requests.
You might also want to look at WireShark which is a network traffic logging tool; you could have used this to see that your UDP datagram most probably WAS being sent but there was no response was generated by the server.
You could also use netstat on the server to see that it isn’t listening on UDP port 80.