首页 > 代码库 > HttpClient
HttpClient
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(connectionTimeout);
HttpClientParams params = new HttpClientParams();
params.setContentCharset("UTF-8");
params.setSoTimeout(soTimeout);
client.setParams(params);
PostMethod method = null;
try {
method = new PostMethod(URL);//URL请求地址
NameValuePair[] nvp= new NameValuePair[]{
new NameValuePair("parameter",parameter)
};//parameter为json参数
method.setRequestBody(nvp);
client.executeMethod(method);
int status = method.getStatusCode();
String response = "";
if (status == 200) {
response = new String(method.getResponseBodyAsString().getBytes("UTF-8"));
return response.trim().endsWith("1");//是否以“1”结束
}else{
throw new Exception("" + status);
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception("" + e.getMessage());
} finally {
if (method != null)
method.releaseConnection();
}