http get请求
2016-03-02 13:09
746 查看
http get 请求方式
代码
代码
import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; /** * * GET接口服务的JAVA示例 * */ public class HttpGetApiInvoker { private final static String URL = "http://test/url"; private final static String REQUEST_CHARSET = "UTF-8"; private final static String RESPONSE_CHARSET = "UTF-8"; private final static int CONNECT_TIMEOUT = 5000; //调用连接超时时间 private final static int REQUEST_TIMEOUT = 10000; //调用获取数据超时时间 public static void main(String[] args) throws Exception{ HttpGetApiInvoker httpGetApiInvoker= new HttpGetApiInvoker(); Map<String, Object> parameterList = httpGetApiInvoker.prepareParameter(); String result = httpGetApiInvoker.invoke(parameterList); System.out.println(result); } /*** * 准备业务参数 * @return */ private Map<String, Object> prepareParameter(){ Map<String, Object> parameterMap = new HashMap<>(); parameterMap.put("name", "xxx"); parameterMap.put("password", "xxxxxxxx"); return parameterMap; } /*** * 拼接调用的URL * @param url * @param parametersMap 业务数据,作为参数 * @return */ private URI buildUri(String url, Map<String, Object> parametersMap) { if (null == parametersMap || parametersMap.isEmpty()) { return URI.create(url); } ArrayList list = new ArrayList(parametersMap.size()); Iterator iterator = parametersMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry entry = (Map.Entry) iterator.next(); list.add(entry.getKey().toString().trim() + "=" + entry.getValue().toString().trim()); } return list.isEmpty() ? URI.create(url) : URI.create(url + "?" + StringUtils.join(list, "&")); } /*** * 没有使用长连接 * @param parameterMap * @return JSON格式的字符串 * @throws IOException */ private String invoke(Map<String, Object> parameterMap) throws IOException { HttpGet httpGet = null; String result = ""; try { //拼接URL httpGet = new HttpGet(buildUri(URL,parameterMap)); //设置超时时间 RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(CONNECT_TIMEOUT) .setSocketTimeout(REQUEST_TIMEOUT) .build(); httpGet.setConfig(requestConfig); httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=" + REQUEST_CHARSET); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); //返回值不是200,进行错误处理 if(HttpStatus.SC_OK != httpResponse.getStatusLine().getStatusCode()){ //相关处理 if(null != httpGet) { httpGet.abort(); } return result; } //获取结果 result = EntityUtils.toString(httpEntity, RESPONSE_CHARSET); }catch(Exception e){ if(null != httpGet){ httpGet.abort(); } throw e; } return result; } }项目依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.3.2 </version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version> 4.3.6</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> <version>4.4</version> </dependency>
相关文章推荐
- iOS网络开发(2)使用NSURLConnection
- HttpContext详解
- 什么是网络爬虫(Spider) 程序
- HttpServlet---getLastModified与缓存
- tcpdump的使用
- KVM 网络设置
- (转)几种开放源码的TCPIP协议栈概述
- TCP三次握手和四次挥手详解
- 如何更高效地使用 OkHttp
- iOS网络开发(1)写代码前需要了解的
- 卷积神经网络(深度学习)一些问题总结
- ASIHTTPRequest
- 卷积神经网络
- Myeclipse中的TCP/IP Monitor使用
- STL之list容器详解 http://www.cnblogs.com/scandy-yuan/archive/2013/01/08/2851324.html
- 计算机网络的各个分层
- iOS开发——网络编程OC篇&Socket编程
- TCP、UDP、IP 协议分析
- iOS实际网络检测框架RealReachability
- httpd-2.4.18+php-5.6.18+nagios-4.1.1+ nagiosgraph-1.5.2 installation on rehl6.4