programming a client to send a HTTP request message to a server with a UDP connection in Java
I need a client to send a http request message to a server but I don't know how to do it in UDP, using http 1.1
It needs to have a specific word in the method field, a specific name in the url field and a specific integer in the version field.
So if I wanted to send a http request message that had "inform" in the method field, "host1" in the url field and 40000 in the version field, how could I do this?
This is what I have now but it doesn't use http, I don't know how to change it.
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("THIS-PC");
byte[] sendData = new byte[128];
byte[] rece开发者_StackOverflow社区iveData = new byte[128];
System.out.println("Enter:");
String request ="INFORM ";
String sentence = inFromUser.readLine();
// request.concat("/r/n");
request.concat(sentence);
// request.concat("/r/n");
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 40075);
clientSocket.send(sendPacket);
System.out.println("Packet sent");
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
System.out.println("Packet received");
String reply = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + reply);
clientSocket.close();
You have to implement the IEEE RFC for HTTP protocol? Or just a simulation over UDP?
HTTP is a text-transfer based protocol that runs over TCP/IP. HTTP default port is 80 but you "can" run a Datagram service through that port.
Losing some particular and helpful ways of the TCP/IP, such as delivering guarantee, order and correct serialization. Do you really have to do that?
Even if done this, you will also need to write a "service" that listens on that port using UDP, all of web-servers works with TCP as this is default and specified in RFC.
Also, read this for more info about:
http://www.w3.org/Protocols/rfc2616/rfc2616.html
Or if you just want a simple app that send and receive data through UDP there are several examples on how to do that on the web.
Sorry if none of this helps.
(a) You would need an HTTP server that spoke UDP. Do you really have that?
(b) You would need to implement the HTTP protocol in this client code. I don't see any attempt to do that. All you're doing is sending lines read directly from the console. No HTTP at all.
精彩评论