import java.io.IOException;

 import net.sf.json.JSONObject;

 import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; public class TestGetRequest { /**
* @param args
*/
public static void main(String[] args) { String url = "http://www.ecmwf.int/plots/get-map/";
String param = "{\"product\":{\"product\":{\"name\":\"public_plots\",\"package\":\"medium\"},\"values\":{\"time\":[\"2016032012\",48,\"2016032212\"],\"parameter\":\"Wind 850 and mslp\",\"area\":\"Asia\"}}}";
System.out.println(param);
JSONObject object = JSONObject.fromObject(param);
JSONObject result = doPost(url, object);
System.out.println(result);
JSONObject imgResult = (JSONObject) result.get("result");
System.out.println(imgResult.get("url"));
} public static String doGet(String url, String charset) throws Exception {
/*
* 使用 GetMethod 来访问一个 URL 对应的网页,实现步骤: 1:生成一个 HttpClinet 对象并设置相应的参数。
* 2:生成一个 GetMethod 对象并设置响应的参数。 3:用 HttpClinet 生成的对象来执行 GetMethod 生成的Get
* 方法。 4:处理响应状态码。 5:若响应正常,处理 HTTP 响应内容。 6:释放连接。
*/
/* 1 生成 HttpClinet 对象并设置参数 */
HttpClient httpClient = new HttpClient();
// 设置 Http 连接超时为5秒
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(
5000);
/* 2 生成 GetMethod 对象并设置参数 */
GetMethod getMethod = new GetMethod(url);
// 设置 get 请求超时为 5 秒
getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
// 设置请求重试处理,用的是默认的重试处理:请求三次
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
String response = "";
/* 3 执行 HTTP GET 请求 */
try {
int statusCode = httpClient.executeMethod(getMethod);
/* 4 判断访问的状态码 */
if (statusCode != HttpStatus.SC_OK) {
System.err.println("请求出错: " + getMethod.getStatusLine());
}
/* 5 处理 HTTP 响应内容 */
// HTTP响应头部信息,这里简单打印
Header[] headers = getMethod.getResponseHeaders();
for (Header h : headers)
System.out
.println(h.getName() + "------------ " + h.getValue());
// 读取 HTTP 响应内容,这里简单打印网页内容
byte[] responseBody = getMethod.getResponseBody();// 读取为字节数组
response = new String(responseBody, charset);
System.out.println("----------response:" + response);
// 读取为 InputStream,在网页内容数据量大时候推荐使用
// InputStream response = getMethod.getResponseBodyAsStream();
} catch (HttpException e) {
// 发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("请检查输入的URL!");
e.printStackTrace();
} catch (IOException e) {
// 发生网络异常
System.out.println("发生网络异常!");
e.printStackTrace();
} finally {
/* 6 .释放连接 */
getMethod.releaseConnection();
}
return response;
} public static JSONObject doPost(String url, JSONObject json) {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
JSONObject response = null;
try {
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
s.setContentType("application/json");// 发送json数据需要设置contentType
post.setEntity(s);
HttpResponse res = client.execute(post);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = res.getEntity();
String result = EntityUtils.toString(res.getEntity());// 返回json格式:
response = JSONObject.fromObject(result);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return response;
} }

最新文章

  1. 【踩坑速记】二次依赖?android studio编译运行各种踩坑解决方案,杜绝弯路,总有你想要的~
  2. [BI项目记]-新任务创建
  3. 链表&LRU
  4. BZOJ2157: 旅游
  5. 2dtoolkit获取sprite像素大小的方法
  6. 微软IIS对http keep-alive的“霸道”处理
  7. jQuery基础之(六)jQuery浏览器的兼容性(以CSS3特写兼容举例)
  8. Python脚本控制的WebDriver 常用操作 <九> 定位一组对象
  9. To get TaskID's Integer ID value from the GUID in SharePoint workflow
  10. 一个CSS小测试
  11. Java基础知识强化40:StringBuffer类之StringBuffer的替换功能
  12. CSS选择器列表
  13. 经典回忆Effective C++ 1
  14. SQL Server比较2table字段的差异
  15. DbContext 和 ObjectContext两者的区别
  16. mongoDB Liunx下安装及配置
  17. Windows7 VS2015 下编译 PythonQt3.2
  18. for...in和for...of循环的区别
  19. 微信 小程序组件 加入购物车全套 one js
  20. OS之进程管理---实时CPU调度

热门文章

  1. Highest Frequency Letters
  2. 【Python】【demo实验16】【练习实例】【打印所有水仙花数】
  3. PBOC第八部分和第十一部分关于TYPEA总结(二)——传输协议(ISO14443-4)
  4. hdu 6025(女生赛)
  5. pat l2-14 列车调度 dilworth+nlog(n)最长上升子序列
  6. 【vue】computed 和 watch
  7. O063、NFS Volume Provider(Part II)
  8. python爬虫redis-ip代理池搭建几十万的ip数据--可以使用
  9. jmeter压测实践
  10. Android Jetpack之WorkManager: 观察结果