67,513
社区成员
发帖
与我相关
我的任务
分享
private static String httpPost(String url,String data){
HttpClient clientP1 = new HttpClient();
PostMethod post = new PostMethod(url);
post.setRequestHeader("Accept-Encoding", "gzip");
System.out.println("-1-请求地址 "+url);
String returnStr = null;
try {
RequestEntity entity = new StringRequestEntity(data, "application/x-www-form-urlencoded", "utf-8");
post.setRequestEntity(entity);
int code = clientP1.executeMethod(post);
if (code == HttpStatus.SC_OK){
returnStr = new String(post.getResponseBodyAsString());
}
// 打印响应信息
System.out.println("-2-请求状态 "+code);
} catch (ConnectTimeoutException e) {
returnStr = "{\"repCode\":\"9999\",\"repInfo\":{\"success\":\"网络请求超时!\"}}";
e.printStackTrace();
} catch (SocketTimeoutException e) {
returnStr = "{\"repCode\":\"9999\",\"repInfo\":{\"success\":\"网络响应超时!\"}}";
e.printStackTrace();
} catch (Exception e) {
returnStr = "{\"repCode\":\"9999\",\"repInfo\":{\"success\":\"网络请求发生异常,请稍后重试!\"}}";
// 协议错误
e.printStackTrace();
}finally {
post.releaseConnection();
}
System.out.println("-3-请求结果 "+returnStr);
return returnStr;
}