I am trying to implement a server client simulation using apache.httpcomponents . The following is the code for the client
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://localhost:8080");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("productionpart", this.getName()));
nvps.add(new BasicNameValuePair("condition", p.Broken+""));
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
System.out.println(response2.getStatusLine());
//HttpEntity entity2 = response2.getEntity();
// do something useful with the response body
// and ensure it is fully consumed
//EntityUtils.consume(entity2);
} finally {
response2.close();
}
This code works. However I am unable to figure out a way to get my entity data on the server side using apache.httpcomponents. I was able to find a code online that confirms that the server is getting the httprequests
int port = 8080;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 8080");
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String number = br.readLine();
System.out.println("Message received from client is "+number);
OutputStream os = socket.getOutputStream();
Scanner sc=new Scanner(System.in);
String inp=null;
String t="HTTP/1.1 200 OK\r\n";
byte[]s=t.getBytes("UTF-8");
os.write(s);
t="Content-Length: 788\r\n";
s=t.getBytes("UTF-8");
os.write(s);
t="Content-Type: text/html\r\n\r\n";
s=t.getBytes("UTF-8");
os.write(s);
}
However I cannot access the entity data this way it simply outputs the length and data type of the content. Is there anyway to access the data in httppost request from the server?