HttpClient设置超时时间 PostMethod

HttpClient设置超时时间 PostMethod,第1张

HttpClient设置超时时间 PostMethod
//设置超时时间
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(5000);
public String clearCacheHttp(){
	//1.构造HttpClient的实例
	HttpClient httpClient = new HttpClient();
	httpClient.getParams().setContentCharset("utf-8");
	//设置超时时间
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(5000);

	//2.构造PostMethod的实例
	String url = httpServerGlobal;
	PostMethod postMethod = new PostMethod(url);
	//like12 add,20160511,中文转码 //在头文件中设置转码
	postMethod.addRequestHeader("Content-Type",
			"application/x-www-form-urlencoded;charset=utf-8");

	//3.把参数值放入到PostMethod对象中
	//方式1:
	NamevaluePair[] data = {
		new NamevaluePair("queryMode", "clearCache")
	};
	postMethod.setRequestBody(data);

	try {
		// 4.执行postMethod,调用http接口
		httpClient.executeMethod(postMethod);//200

		

		//6.处理返回的内容
		
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		//7.释放连接
		postMethod.releaseConnection();
	}
	
	return "success";
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5693052.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存