import java.util.*;
public class HttpTools {
private static HttpTools instance = new HttpTools():
private HttpClient httpClient = getHttpClient();
public String post(String url, String info) throws ParseException, IOException {
HttpPost post = new HttpPost(url):
StringEntity entity = new Stringentity(info, "UTF-8");
HttpResponse response = httpClient.execute(post);
HttpEntity httpEntity = response.getEntity();
String returnStr = null;
if(httpEntity != null) {
returnStr = EntityUtils.toString(httpEntiy);
EntityUtils.consume(httpEntity);
}
return returnStr;
}
public Strin get(String url) throws ParseException, IOException {
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
HttpGet httpGet = new HttpGet(url);
HttpResponse reponse = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
String returnStr = null;
if(httpEntity != null) {
returnStr = EntityUtils.toString(httpEntiy);
EntityUtils.consume(httpEntity);
}
}
private HttpClient getHttpClient() {
PoolingClientConnectionManager cm = new PoolingClientConnectionManager();
cm.setMaxTotal(10);
HttpClient httpClient = new DefaultHttpClient(cm);
return httpClient();
}
public static void main(String[] args) {
System.out.println("Hello, World...");
}
}
HttpClient 4 简单示例
最新推荐文章于 2019-04-18 12:26:16 发布
本文介绍了一个简单的HTTP工具类实现,包括POST和GET请求的方法。该工具使用Java编写,并利用了HttpClient进行网络通信。
1万+

被折叠的 条评论
为什么被折叠?



