Commons-httpclient的使用技巧,更方便的模拟http请求!
2011-06-16 10:41
337 查看
本文来自:http://lavasoft.blog.51cto.com/62575/168276/
另外一个好文章:
http://www.blogjava.net/willpower88/archive/2008/04/14/192679.html
需要jar包:
common-codec
common.httpclient
注意代码中设置编码,防止出现乱码
另外一个好文章:
http://www.blogjava.net/willpower88/archive/2008/04/14/192679.html
需要jar包:
common-codec
common.httpclient
public static void main(String[] args) { //请求地址 String url = "http://localhost:8080/moneyManager/game"; doGet(url); Map<String,String> params = new HashMap<String,String>(); params.put("gameName", "仙剑奇侠传5"); params.put("clickCount", "4"); //String str = doPost(url,params); //System.out.println(str); } public static void doGet(String url){ String queryString = ""; String response = null; HttpClient client = new HttpClient(); //防止乱码 client.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); //设置编码的三种方式 //postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8"); //postMethod.addRequestHeader("Content-Type","text/html;charset=UTF-8"); //postMethod.setRequestHeader("Content-Type", "text/html;charset=UTF-8"); HttpMethod method = new GetMethod(url); try { if (queryString!=null&&!"".equals(queryString)) method.setQueryString(URIUtil.encodeQuery(queryString)); //method.set client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { response = method.getResponseBodyAsString(); } } catch (URIException e) { // log.error("执行HTTP Get请求时,编码查询字符串“" + queryString + "”发生异常!", e); } catch (IOException e) { //log.error("执行HTTP Get请求" + url + "时,发生异常!", e); } finally { method.releaseConnection(); } System.out.println(response); } public static String doPost(String url, Map<String, String> params) { String response = null; HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(url); for (Iterator it = params.entrySet().iterator(); it.hasNext();) { } //设置Http Post数据 if (params != null) { HttpMethodParams p = new HttpMethodParams(); for (Map.Entry<String, String> entry : params.entrySet()) { p.setParameter(entry.getKey(), entry.getValue()); } method.setParams(p); } try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { response = method.getResponseBodyAsString(); } } catch (IOException e) { //log.error("执行HTTP Post请求" + url + "时,发生异常!", e); System.out.println("执行HTTP Post请求" + url + "时,发生异常!"); } finally { method.releaseConnection(); } return response; }
注意代码中设置编码,防止出现乱码
相关文章推荐
- 使用HttpClient4.3.1模拟Http请求与无信任证书访问Https
- 使用httpclient模拟http请求其他系统的接口
- HttpClientUtil [使用apache httpclient模拟http请求]
- HttpClient和HtmlUnit的比较总结以及使用技巧(二)
- commons-httpclient简单使用
- 使用fiddler模拟http请求
- ROSETTA使用技巧随笔--PyMOL实时观测ROSETTA模拟过程中的结构变化
- 使用Apache中的HttpClient的实例CloseableHttpClient进行http请求
- 使用fiddler模拟http请求
- java----使用socket模拟简单的http请求服务器,响应简单的文件请求操作
- 使用HttpClient4发送 http请求
- HttpClient4.5 使用http连接池发送http请求深度示例
- Unity 游戏开发技巧集锦之使用cookie类型的纹理模拟云层的移动
- Java爬虫入门简介(四)——抓包工具的使用以及使用HttpClient模拟用户登录的访问
- 使用HttpClient模拟POST请求
- java使用httpclient模拟post请求和get请求示例
- 使用HttpClient发送http请求,并解析从服务器端返回的数据
- java使用HttpURLConnection和HttpClient分别模拟get和post请求以及操作cookies
- java使用httpclient简单模拟登陆微信公众开放平台
- java使用HttpClient发送http请求