Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading bytes from Socket?

Sanjit Singh
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,
I am trying to achieve something and I am not sure if it is the right way of doing it.
My client program is as follows:
URL url = new URL("http://localhost:12000");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
String test = "Normally here would be contents of an XML file";
byte[] bytes = test.getBytes("UTF8");
OutputStream out = con.getOutputStream();
Due to some reasons, the client code cannot be changed. I need to send the contents as a byte array over http using the POST method. Now, I am interested in creating my own Server which takes this post request and stores the contents in a string. Could someone kindly help me with this. I have written the following so far:
ServerSocket serverSocket = new ServerSocket(12000);
System.out.println("HTTP Server (only POST implemented) is ready and is listening on Port Number 12000 \n");
Socket clientSocket = serverSocket.accept();
BufferedInputStream in = new BufferedInputStream(clientSocket.getInputStream());
//kindly help here.code for manipulating the bytes. storing stuff in byte array and storing the contents received in a string
Please note that I have tried the BufferedReader and readLine() method, but I do not receive anything and it doesnt seem to work. Any code would be very helpful. I am nterested in extracting the xml file that is in the body of the http post request and storing this in a string on the server.
Thanks in advance!
Lewin Chan
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What can I say, other than goto and get the examples. Buy the book too
If I'm not mistaken chapter 12 has the src for a v.simple threaded http server.
This should give you the basis for your server program so that you can accept POST requests.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic