0
如何設置HttpDefaultClient
的暫停時間?如何設置HttpDefaultClient的超時期限?
對於下面的代碼,我想設置一些時間,以便它不會掛起更多的時間。
String methodName = "getResponseFromGlobalService";
logger.info(methodName + " started " + "with" + restUrl + "inputs" + jsonObject.toString());
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(restUrl);
StringBuffer outputDocument = new StringBuffer();
StringEntity input1 = new StringEntity(jsonObject.toString());
input1.setContentType("application/json");
postRequest.setEntity(input1);
HttpResponse response = httpClient.execute(postRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String output;
logger.info("Output from Server .... \n");
while ((output = br.readLine()) != null) {
outputDocument.append(output);
}
JSONObject json = (JSONObject) JSONSerializer.toJSON(outputDocument.toString());
logger.info(methodName + " ended ");
return json;
重複http://stackoverflow.com/questions/3000214/java-http-client-request-with-defined-timeout – aksappy
的請考慮upvoting或接受的答案,如果它幫助你解決了這個問題。否則,請提供信息。在什麼不工作。謝謝! –