使用HttpClient请求另一个项目接口获取内容
2016-09-09 11:23
344 查看
我们在实际开发中常常会遇到这种问题:在一个项目中需要访问另一个项目的接口获取需要的内容。因此我们就涉及到了HttpClient请求的问题,主要包括两种方式:HttpPost和HttpGet两种。
一、HttpGet请求
public String doHttpGet() {
// 需要访问的接口路径
String url = "http://124.114.27.135:3110/checkAuto/getLevel?levelId=13";
// 配置请求信息(请求时间)
RequestConfig rc = RequestConfig.custom().setSocketTimeout(5000)
.setConnectTimeout(5000).build();
// 获取使用DefaultHttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 返回结果
String result = null;
try {
if (url != null) {
// 创建HttpGet对象,将URL通过构造方法传入HttpGet对象
HttpGet httpget = new HttpGet(url);
// 将配置好请求信息附加到http请求中
httpget.setConfig(rc);
// 执行DefaultHttpClient对象的execute方法发送GET请求,通过CloseableHttpResponse接口的实例,可以获取服务器返回的信息
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 得到返回对象
HttpEntity entity = response.getEntity();
if (entity != null) {
// 获取返回结果
result = EntityUtils.toString(entity);
}
} finally {
// 关闭到客户端的连接
response.close();
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭http请求
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
一、HttpGet请求
public String doHttpGet() {
// 需要访问的接口路径
String url = "http://124.114.27.135:3110/checkAuto/getLevel?levelId=13";
// 配置请求信息(请求时间)
RequestConfig rc = RequestConfig.custom().setSocketTimeout(5000)
.setConnectTimeout(5000).build();
// 获取使用DefaultHttpClient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 返回结果
String result = null;
try {
if (url != null) {
// 创建HttpGet对象,将URL通过构造方法传入HttpGet对象
HttpGet httpget = new HttpGet(url);
// 将配置好请求信息附加到http请求中
httpget.setConfig(rc);
// 执行DefaultHttpClient对象的execute方法发送GET请求,通过CloseableHttpResponse接口的实例,可以获取服务器返回的信息
CloseableHttpResponse response = httpclient.execute(httpget);
try {
// 得到返回对象
HttpEntity entity = response.getEntity();
if (entity != null) {
// 获取返回结果
result = EntityUtils.toString(entity);
}
} finally {
// 关闭到客户端的连接
response.close();
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// 关闭http请求
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
相关文章推荐
- 使用HttpClient请求另一个项目接口获取内容
- 使用HttpClient请求另一个项目接口获取内容
- php使用fsockopen函数发送post,get请求获取网页内容的方法
- iOS项目开发实战——使用同步请求获取网页源代码
- SpringMVC 请求路径结尾存在.使用@PathVariable访问路径内容,路径参数获取不准确的问题
- spring一个项目调用另一个项目接口的方法,HttpClient调用
- WebApi系列~实际项目中如何使用HttpClient向web api发异步Get和Post请求并且参数于具体实体类型
- 使用Twitter接口获取twitter内容
- iOS项目开发实战——使用同步请求获取网页源码
- 框架 day78 涛涛商城项目-CMS内容管理,httpclient使用,首页ad展示
- 一个项目如何请求访问,另一个封装好的接口项目的数据
- android 项目中使用到的网络请求框架以及如何配置好接口URL
- 最近做一个新闻类项目,用到调用新浪微博接口,经过研究测试整理步骤如下:先根据此内容获取App Key和Secret Key
- 使用HttpClient配置代理服务器模拟浏览器发送请求调用接口测试
- android使用HttpClient和URLConnection获取网页内容
- frameset里的一个frame1使用js获取另一个frame2里的内容
- android 项目中使用到的网络请求框架以及怎样配置好接口URL
- 【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目
- 获取iframe中的内容、查找获取指定元素(关于用c++调用WEBBROWSER控件,使用相关接口操作web页面元素的一些方法)
- 接口测试——HttpClient工具的https请求、代理设置、请求头设置、获取状态码和响应头