我有一个RESTful服务器,它从客户端接受http POST输入,在服务器上投票.我已经为客户端使用了Apache HTTPClient.
public boolean vote() {
HttpClient client = new DefaultHttpClient(getHttpParameters());
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout Limit
HttpResponse response;
try {
HttpPost post = new HttpPost("http://127.0.0.1:8080/ws/");
StringEntity se = new StringEntity("{ \"song_id\" : \"2\" }");
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
post.setEntity(se);
response = client.execute(post);
if (response != null) {
InputStream in = response.getEntity().getContent(); // Get the data in the Entity
String result = convertStreamToString(in);
return true;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
public static HttpParams getHttpParameters() {
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 30000;
HttpConnectionParams.setConnectionTimeout(httpParameters,
timeoutConnection);
int timeoutSocket = 30000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
return httpParameters;
}
当我点击投票按钮继续,经过少数投票(如7-8),我得到java.net.SocketTimeoutException:读取超时异常.
当我搜索原因时,我发现这是因为客户端在超时时间内没有得到服务器响应.但问题是,当我使用其他应用程序,如Chrome REST控制台或JMeter,我可以在相同的服务器上投票赞成相同的参数和路径.我的java代码有什么问题吗?请帮我弄清楚.以下是我的堆栈跟踪:
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:166)
at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer.java:90)
at org.