新项目中使用新版的apache HttpClient调用http接口,贴上代码如下
import java.io.InputStream; import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; 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.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.codehaus.jackson.map.ObjectMapper; @SuppressWarnings("unchecked") private static Map<String, String> doGet(String address) { Map<String, String> coordinate = new HashMap<String, String>(); if (StringUtils.isNotEmpty(address)) { try { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("test", 123), new UsernamePasswordCredentials("test", "test")); CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider) .build(); Map<String, String> paramsMap = new LinkedHashMap<String, String>(); paramsMap.put("output", "json"); String paramsStr = BaiduSnCal.toQueryString(paramsMap); String wholeStr = new String("" + paramsStr); HttpGet httpGet = new HttpGet(wholeStr); httpGet .addHeader("Referer", ""); // 设置代理 HttpHost proxy = new HttpHost("", 8080); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); httpGet.setConfig(config); CloseableHttpResponse resp = httpClient.execute(httpGet); InputStream in = resp.getEntity().getContent(); StringWriter w = new StringWriter(); IOUtils.copy(in, w, BaiduSnCal.CHARSET); String result = w.toString(); System.out.println(result); Map<String, Object> returnMap = mapper.readValue(result, Map.class); System.out.println(((Map<String, Object>) returnMap.get("result")).get("location")); } catch (Exception e) { LOGGER.error("error : ", e); } } return coordinate; } public static void main(String[] args) { try { getCoordinateByAddress("test"); } catch (Exception e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") private static void doPost(String address) { String url = ""; try { CloseableHttpClient httpClient = HttpClients.custom().build(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Referer", ""); List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); nvps.add(new BasicNameValuePair("", "")); nvps.add(new BasicNameValuePair("output", "json")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps, ENCODING); httpPost.setEntity(entity); CloseableHttpResponse resp = httpClient.execute(httpPost); InputStream in = resp.getEntity().getContent(); StringWriter w = new StringWriter(); IOUtils.copy(in, w, ENCODING); String result = w.toString(); System.out.println(result); Map<String, Object> returnMap = mapper.readValue(result, Map.class); System.out.println(((Map<String, Object>)returnMap.get("result")).get("location")); System.out.println(returnMap); } catch (Exception e) { e.printStackTrace(); } }
相关推荐
目的是为了替换难用的Apache HttpClient。easy-okhttp是对OkHttp网络框架封装,提供文件上传和下载,表单(含文件)提交,链式调用,支持HTTPS和自定义签名证书等特性。 OkHttp网络框架的流行始于Android,但是在Java...
apache httpclient检测插件 buildscript { repositories { jcenter() } dependencies { classpath "io.github.lizhangqu:plugin-apache-httpclient-detect:1.0.7" } } apply plugin: 'apache.httpclient....
使用 HttpClient,调用http接口, 使用HttpClient JAVA发送http get/post请求,
下载这个工具类,然后直接调用createHtmlPage方法就可以了。最关键一点是,网上的方法没有一个解决了乱码问题,费了我不少功夫。
启用 Web 服务的 HTTP 和 HTTPS (SSL) 调用 它支持表单参数、文件上传和自定义正文实体 无需丑陋的字符串连接即可轻松添加路由参数 本机支持基本身份验证 每个请求的可定制标头 完全了解代理。 可定制的超时时间 ...
这是一个基于Apache HttpClient实现的一个异步Http客户端。 特征: 1.异步以回调实现http请求。 2.使用线程池并发请求限制资源的使用。 3.支持Http的所有谓词。 4.封装请求参数,使调用更加容易。 5.可配置化的实现...
在微服务框架之外的系统中,我们经常会遇到使用httpClient进行接口调用的问题,除了进行白名单的设置,很多时候我们需要在接口调用的时候需要身份认证。翻了一下官方文档,解决方法很多,但是都不太符合实际业务场景...
前端直接调用图床API接口报错,利用后端接口进行文件转发实现接口反向代理 使用Spring Boot内置的`RestTemplate`来实现。以下是一个基于`RestTemplate`转发文件上传请求到目标服务的示例 主要运用了以下技术: 1. ...
hiveserver2的jdbc调用时需要用到的两个包httpclient+httpcore. org.apache.http.*
3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。 3.调用HttpClient...
Java 业务开发常见错误 100 例 05丨HTTP调用:你考虑到超时、...如果不使用 Spring Cloud,而直接使用 Spring Boot 进行微服务开发的话,可能会直接使用 Java 中最常用的 HTTP 客户端 Apache HttpClient 进行服务调用。
目前有Netty 和 Apache Compoenet两种实现。本次上传移除了Netty实现。主要解决生产环境中同步httpclient造成的IO阻塞问题。同步http请求将导致 tomcat 的业务线程被阻塞。一旦某接口网络出现问题,可能会阻塞tomcat...
附件包含一个根据身份证号码调用必应接口返回个人身份证信息的例子,有JAR包和代码例子,可以直接拿来使用。
比如apache的httpClient包,非常优秀的Okhttp,jersey client。 这些http开源框架的接口使用相对来说,都不太一样。不管选哪个,在我这个场景里来说,我都不希望在调用每个第三方的http api时写上一堆http调用代码。...
含有开发过程中 接口调用,网页爬虫,需要的jar...HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议
该压缩包搜集了所有http相关的jar包,包含httpclient httpresponse httprequest已经其它相关的类,分享给大家,欢迎下载
我主要使用Apache Http Client,因此最初仅使用来自Apache的http客户端创建了该项目。 一段时间后,我发现有更多的Java客户端,并且还有一些基于Kotlin和Scala的客户端。 配置ssl / tls可能很困难,每个客户端都需要...
A http request engine based on Volley and OkHttp, giving up Apache HttpClient request. supports image loading, restful api requesting, and file downloading. READ MORE Dependency binary on jcenter ...
jar包有10个分别为:commons-codec-1.10.jar,commons-logging-1.2.jar,fluent-hc-4.5.5.jar,httpclient-4.5.5.jar,httpclient-cache-4.5.5.jar,httpclient-win-4.5.5.jar,httpcore-4.4.9.jar,...
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396) at org.codehaus.xfire.transport.http.CommonsHttpMessageSender.send(CommonsHttpMessageSender.java:369) at org.codehaus....