我正在尝试使用apache.httpcomponents实现服务器客户端模拟。以下是客户端的代码
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();
}
此代码有效。但是,我无法找到一种使用apache.httpcomponents在服务器端获取我的实体数据的方法。我能够在线找到确认服务器正在获取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);
}
但是,我无法以这种方式访问实体数据,它仅输出内容的长度和数据类型。无论如何,是否可以从服务器访问httppost请求中的数据?