1 package com.itNoob.httpClient;
2
3 import org.apache.commons.httpclient.HttpClient;
4 import org.apache.commons.httpclient.methods.PostMethod;
5 import org.apache.commons.httpclient.params.HttpMethodParams;
6
7 public class HTTPClient {
8
9 public static void main(String[] args) {
10 // 请求接口地址
11 String url = "http://v.juhe.cn/historyWeather/province";
12 // 请求参数
13 String key = "c60f89ced863679ebe086b9971b38957";
14
15 HttpClient httpclient = null;
16 PostMethod post = null;
17 try {
18 // 创建连接
19 httpclient = new HttpClient();
20 post = new PostMethod(url);
21 // 设置编码方式
22 post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
23 // 添加参数
24 post.addParameter("key", key);
25 // 执行请求
26 httpclient.executeMethod(post);
27 // 接口返回信息
28 String info = new String(post.getResponseBody(), "UTF-8");
29 System.out.println("resultJSON:" + info);
30 } catch (Exception e) {
31 e.printStackTrace();
32 } finally {
33 // 关闭连接,释放资源
34 post.releaseConnection();
35 }
36 }
37 }
httpClient使用到的jar包:http://www.wmzhe.com/soft-32814.html