谈到httpClient相信大家都不陌生,网上也有一大推别人总结的。HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。
详细请参考文档:HttpClient
我们在对数据进行请求的时候经常使用。
前不久在做一个百度地图定位的(通过GPS判断你在某个学校,但是并不是每个学校地图上都有,而且如何确定范围呢?)
类似于饿了么,我一直在想它为什么能定位到具体的某个宿舍呢?后来发现是利用了百度地图LBS云(当然这是我个人实现的方案):
1. 调用手机GPS定位(或者浏览器定位,浏览器定位不会很精确,一般会定位到区,街道不会很详细)。获取经纬度。
2. 上传经纬度,以及名称(具体请参考百度api接口文档),比如武汉纺织大学,这样在你的百度LBS云,中就有了这个点的信息。
3. 当你手机要获取具体定位时,手机GPS获取经纬度,然后调用接口(具体请参考百度api接口文档),获取这个点为中心,半径为50m(自己写),百度LBS云是否有这个点,有返回list信息,这样你就获取了你所在位置在哪了。
这样你在配送地址就可以自己定位了。我大胆猜测,我们用外卖软件的时候,首先它已经定位到了一个比较精确的位置,比如武汉纺织大学,但是得自己输入楼号,比如输入5号教学楼,这样他就会把,你位置的经纬度,还有武汉纺织大学5号教学楼,传到服务器上,(如果有很多人打错了字,或者其他,估计有个特定的算法,比如取数据名称相同最多的3个)。这样我们下次定位的时候,可能就在这个50m范围内,就能推荐这个地址了(武汉纺织大学5号教学楼)。
为什么要讲这个呢?我偏题了吗?
最近在做毕设,浏览器定位,我想直接用Ajax请求,url就写百度LBS云接口即可。
但是出了一个问题,我请求跨域了(本地服务器想要访问百度服务器),虽然网上有解决跨域的方法,但是后来我想了一个解决方法,我为什么一定要用JS来访问百度LBS云接口呢?
我JS获得了经纬度,位置名称,直接传到后台服务器,然后后台通过HttpClient来请求百度云服务器不就好了吗?然后将数据返回给前端JS。
接下来我就来通过HttpClient 工具类,来进行请求,来实现百度地图定位的这个功能。
- Request.java
package com.http;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Requst分装类,封装一些信息
* 作者:Administrator<br>
* 版本:1.0<br>
* 创建日期:下午9:24:43<br>
*/
public class Request {
/**请求属性*/
private Map<String, String> properties;
/**请求参数*/
private Map<String, String> params ;
/**请求头部*/
private Map<String, String> headers;
private byte[] postData;
/**是否同步*/
private boolean sync=false;
/**
* 获得设置的属性
* @param property
* @return
*/
public String getProperty(String property) {
if(properties ==null){
return null;
}
return properties.get(property);
}
/**
* 设置属性
* @param propery
* @param value
*/
public void setProperty(String propery,String value) {
if(properties==null){
properties = new LinkedHashMap<String, String>();
}
properties.put(propery, value);
}
/**
* 设置参数
* @param params
*/
public void setParam(String param,String value) {
if(params==null){
params = new LinkedHashMap<String, String>();
}
params.put(param, value);
}
public Map<String, String> getHeaders() {
return headers;
}
/**
* 设置头部信息
* @param header
* @param value
*/
public void setHeader(String header, String value) {
if (headers == null) {
headers = new LinkedHashMap<String, String>();
}
headers.put(header, value);
}
public Map<String, String> getParams() {
return params;
}
public String getHeader(String header) {
if (headers == null) {
return null;
}
return headers.get(header);
}
public Map<String, String> getProperties() {
return properties;
}
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public void setParams(Map<String, String> params) {
this.params = params;
}
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
public byte[] getPostData() {
return postData;
}
public void setPostData(byte[] postData) {
this.postData = postData;
}
public boolean isSync() {
return sync;
}
public void setSync(boolean sync) {
this.sync = sync;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- Response.java
package com.http;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 响应类
* 作者:Administrator<br>
* 版本:1.0<br>
* 创建日期:下午9:36:03<br>
*/
public class Response {
/**响应码*/
private int statusCode ;
/**响应内容*/
private byte[] content ;
/**响应头部*/
private Map<String, String> headers;
/**
* 设置响应头
*
* @param name
* @param value
*/
public void setHeader(String name, String value) {
if (headers == null) {
headers = new LinkedHashMap<String, String>();
}
headers.put(name, value);
}
/**
* 按指定编码获得响应内容,有些响应乱码了 需要解决乱码问题
* @param encoding
* @return
*/
public String getContentString(String encoding) {
try {
String contentString = new String(getContent(), encoding);
return contentString;
} catch (Exception e) {
System.out.println("不支持编码");
}
return null;
}
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public byte[] getContent() {
return content;
}
public void setContent(byte[] content) {
this.content = content;
}
public Map<String, String> getHeaders() {
return headers;
}
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- HttpClientUtil
package com.http;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.GZIPInputStream;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.NoHttpResponseException;
import org.apache.http.ParseException;
import org.apache.http.ProtocolException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.config.RequestConfig.Builder;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.HttpHostConnectException;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultRedirectStrategy;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EntityUtils;
/**
* HttpClient工具封装类,Post,Get请求,代理请求
* 作者:<br>
* 版本:1.0<br>
* 创建日期:下午9:21:00<br>
*/
public class HttpClientUtil {
/***连接超时时间*/
private Integer connectTimeout =60*1000;
private Integer socketTimeout =180*1000;
private CloseableHttpClient httpClient = null;
private CookieStore cookieStore = new BasicCookieStore();
/** 代理请求 */
public HttpClientUtil(String proxyHost, int proxyPort) {
this(proxyHost, proxyPort, -1, -1, 0, 0, true);
}
/** 默认*/
public HttpClientUtil() {
this(null, 0, -1, -1, 0, 0, true);
}
/** 进行请求无代理设置连接时间 */
public HttpClientUtil(int socketTimeout, int connectTimeout) {
this(null, 0, socketTimeout, connectTimeout, 0, 0, true);
}
/**
*
* @param proxyHost 代理主机地址
* @param proxyPort 代理端口
* @param socketTimeout
* @param connectTimeout
* @param route
* @param maxTotal
* @param followRedirect
*/
public HttpClientUtil(String proxyHost, int proxyPort, int socketTimeout, int connectTimeout, int route, int maxTotal, boolean followRedirect){
Builder builder = RequestConfig.custom();
builder.setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY);
if (followRedirect) {
builder.setCircularRedirectsAllowed(true);
builder.setMaxRedirects(100);
}
if (StringUtils.isNotBlank(proxyHost) && proxyPort > 0) {
builder.setProxy(new HttpHost(proxyHost, proxyPort));
}
if (socketTimeout != -1) {
this.socketTimeout = socketTimeout;
}
builder.setSocketTimeout(this.socketTimeout);
if (connectTimeout != -1) {
this.connectTimeout = connectTimeout;
}
builder.setConnectTimeout(this.connectTimeout);
builder.setConnectionRequestTimeout(this.connectTimeout);
RequestConfig requestConfig = builder.build();
init(requestConfig, route, maxTotal);
}
private void init(RequestConfig requestConfig, int route, int maxTotal) {
X509TrustManager x509mgr = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] xcs, String string) {
}
@Override
public void checkServerTrusted(X509Certificate[] xcs, String string) {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
SSLContext sslContext = null;
try {
sslContext = SSLContext.getInstance("TLS");
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
try {
sslContext.init(null, new TrustManager[] { x509mgr }, null);
} catch (KeyManagementException e) {
e.printStackTrace();
}
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= 5) {
return false;
}
if (exception instanceof NoHttpResponseException) {
return true;
}
if (exception instanceof SSLHandshakeException) {
return false;
}
if (exception instanceof InterruptedIOException) {
return false;
}
if (exception instanceof UnknownHostException) {
return false;
}
if (exception instanceof ConnectTimeoutException) {
return false;
}
if (exception instanceof SSLException) {
return false;
}
if (exception instanceof SocketException) {
return true;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
};
DefaultRedirectStrategy redirectStrategy = new DefaultRedirectStrategy() {
public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
boolean isRedirect = false;
try {
isRedirect = super.isRedirected(request, response, context);
} catch (ProtocolException e) {
e.printStackTrace();
}
if (!isRedirect) {
int responseCode = response.getStatusLine().getStatusCode();
if (responseCode == 301 || responseCode == 302) {
return true;
}
}
return isRedirect;
}
@Override
protected URI createLocationURI(String location) throws ProtocolException {
location = location.replace("|", "%7C");
return super.createLocationURI(location);
}
};
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
if (route > 0 && maxTotal > 0) {
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setDefaultMaxPerRoute(route);
connManager.setMaxTotal(maxTotal);
httpClientBuilder.setConnectionManager(connManager);
}
httpClientBuilder.setSSLSocketFactory(sslsf);
httpClientBuilder.setDefaultCookieStore(cookieStore);
httpClientBuilder.setRedirectStrategy(redirectStrategy);
httpClientBuilder.setRetryHandler(httpRequestRetryHandler);
httpClient = httpClientBuilder.build();
}
public Response sendRequest(Request request) throws Exception {
if (request.getHeader("User-Agent") == null) {
request.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0");
}
request.setHeader("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
request.setHeader("Connection", "keep-alive");
String method = request.getProperty("method").toLowerCase();
String url = request.getProperty("url");
Map<String, String> headers = request.getHeaders();
Map<String, String> params = request.getParams();
List<NameValuePair> formParams = new ArrayList<NameValuePair>();
if (params != null && params.size() != 0) {
formParams = new ArrayList<NameValuePair>();
for (Entry<String, String> entry : params.entrySet()) {
formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
Response response = null;
if ("post".equals(method)) {
byte[] postData = request.getPostData();
if (postData != null) {
response = post(url, postData, headers);
} else {
response = post(url, formParams, headers);
}
} else if ("get".equals(method)) {
response = get(url, formParams, headers);
}
return response;
}
/**
* Get请求
*
* @param url
* @param params
* @return
*/
public Response get(String url, List<NameValuePair> params, Map<String, String> headers) {
Response response = new Response();
try {
HttpGet httpGet = new HttpGet(url);
String encoding = "utf-8";
if (headers != null && headers.size() != 0) {
for (Entry<String, String> entry : headers.entrySet()) {
httpGet.setHeader(entry.getKey(), entry.getValue());
}
String contentType = headers.get("Content-Type");
if (StringUtils.isNotBlank(contentType)) {
if (matcher(contentType, "(charset)\\s?=\\s?(gbk)")) {
encoding = "gbk";
} else if (matcher(contentType, "(charset)\\s?=\\s?(gb2312)")) {
encoding = "gb2312";
}
}
}
if (params != null && params.size() > 0) {
if (httpGet.getURI().toString().indexOf("?") == -1) {
String str = EntityUtils.toString(new UrlEncodedFormEntity(params, encoding));
httpGet.setURI(new URI(httpGet.getURI().toString() + "?" + str));
} else {
String str = EntityUtils.toString(new UrlEncodedFormEntity(params, encoding));
httpGet.setURI(new URI(httpGet.getURI().toString() + "&" + str));
}
}
CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
try {
int statusCode = httpResponse.getStatusLine().getStatusCode();
response.setStatusCode(statusCode);
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
HttpEntity entity = httpResponse.getEntity();
Header[] responseHeaders = httpResponse.getAllHeaders();
for (Header header : responseHeaders) {
response.setHeader(header.getName(), header.getValue());
}
Header header = entity.getContentEncoding();
if (header != null && header.getValue().toLowerCase().equals("gzip")) {
byte[] bytes = IOUtils.toByteArray(new GZIPInputStream(entity.getContent()));
response.setContent(bytes);
} else {
byte[] bytes = getData(entity);
response.setContent(bytes);
}
return response;
} finally {
httpResponse.close();
}
} catch (ConnectTimeoutException e) {
} catch (HttpHostConnectException e) {
} catch (ParseException e) {
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
} catch (URISyntaxException e) {
} catch (Exception e) {
}
return null;
}
/**
* // Post请求
*
* @param url
* @param params
* @return
*/
public Response post(String url, byte[] data, Map<String, String> headers) {
Response response = new Response();
try {
HttpPost httpPost = new HttpPost(url);
if (headers != null && headers.size() != 0) {
for (Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
}
httpPost.setEntity(new ByteArrayEntity(data));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
try {
int statusCode = httpResponse.getStatusLine().getStatusCode();
response.setStatusCode(statusCode);
HttpEntity entity = httpResponse.getEntity();
Header header = entity.getContentEncoding();
if (header != null && header.getValue().toLowerCase().equals("gzip")) {
byte[] bytes = IOUtils.toByteArray(new GZIPInputStream(entity.getContent()));
response.setContent(bytes);
} else {
byte[] bytes = EntityUtils.toByteArray(entity);
response.setContent(bytes);
}
return response;
} finally {
httpResponse.close();
}
} catch (ConnectTimeoutException e) {
} catch (HttpHostConnectException e) {
} catch (UnsupportedEncodingException e) {
} catch (ClientProtocolException e) {
} catch (ParseException e) {
} catch (IOException e) {
} catch (Exception e) {
}
return null;
}
private byte[] getData(HttpEntity entity) throws IOException {
if (entity == null) {
throw new IllegalArgumentException("HTTP entity may not be null");
}
InputStream inputStream = entity.getContent();
if (inputStream == null) {
return null;
}
try {
if (entity.getContentLength() > Integer.MAX_VALUE) {
throw new IllegalArgumentException("HTTP entity too large to be buffered in memory");
}
int i = (int) entity.getContentLength();
if (i < 0) {
i = 4096;
}
ByteArrayBuffer buffer = new ByteArrayBuffer(i);
byte[] tmp = new byte[1024];
int l = -1;
try {
while ((l = inputStream.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
} catch (EOFException e) {
buffer.clear();
tmp = new byte[32];
try {
while ((l = inputStream.read(tmp)) != 1) {
buffer.append(tmp, 0, l);
}
} catch (EOFException e2) {
}
}
byte[] byteArray = buffer.toByteArray();
if (byteArray == null || byteArray.length == 0) {
return buffer.buffer();
}
return byteArray;
} finally {
inputStream.close();
}
}
/**
* // Post请求
*
* @param url
* @param params
* @return
*/
public Response post(String url, List<NameValuePair> params, Map<String, String> headers) {
Response response = new Response();
try {
HttpPost httpPost = new HttpPost(url);
String encoding = "utf-8";
if (headers != null && headers.size() != 0) {
for (Entry<String, String> entry : headers.entrySet()) {
httpPost.setHeader(entry.getKey(), entry.getValue());
}
String contentType = headers.get("Content-Type");
if (StringUtils.isNotBlank(contentType)) {
if (matcher(contentType, "(charset)\\s?=\\s?(gbk)")) {
encoding = "gbk";
} else if (matcher(contentType, "(charset)\\s?=\\s?(gb2312)")) {
encoding = "gb2312";
}
}
}
if (params != null && params.size() > 0) {
httpPost.setEntity(new UrlEncodedFormEntity(params, encoding));
}
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
try {
int statusCode = httpResponse.getStatusLine().getStatusCode();
response.setStatusCode(statusCode);
HttpEntity entity = httpResponse.getEntity();
Header header = entity.getContentEncoding();
if (header != null && header.getValue().toLowerCase().equals("gzip")) {
byte[] data = IOUtils.toByteArray(new GZIPInputStream(entity.getContent()));
response.setContent(data);
} else {
byte[] data = getData(entity);
response.setContent(data);
}
return response;
} finally {
httpResponse.close();
}
} catch (ConnectTimeoutException e) {
} catch (HttpHostConnectException e) {
} catch (UnsupportedEncodingException e) {
} catch (ClientProtocolException e) {
} catch (ParseException e) {
} catch (IOException e) {
} catch (Exception e) {
}
return null;
}
/**
* 获取Response内容字符集
*
* @param response
* @return
*/
public String getContentCharset(HttpResponse response) {
String charset = "ISO_8859-1";
Header header = response.getEntity().getContentType();
if (header != null) {
String s = header.getValue();
if (matcher(s, "(charset)\\s?=\\s?(utf-?8)")) {
charset = "utf-8";
} else if (matcher(s, "(charset)\\s?=\\s?(gbk)")) {
charset = "gbk";
} else if (matcher(s, "(charset)\\s?=\\s?(gb2312)")) {
charset = "gb2312";
}
}
Header encoding = response.getEntity().getContentEncoding();
return charset;
}
/**
* 正则匹配
*
* @param s
* @param pattern
* @return
*/
private boolean matcher(String s, String pattern) {
Pattern p = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE + Pattern.UNICODE_CASE);
Matcher matcher = p.matcher(s);
if (matcher.find()) {
return true;
} else {
return false;
}
}
public Integer getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(Integer connectTimeout) {
this.connectTimeout = connectTimeout;
}
public Integer getSocketTimeout() {
return socketTimeout;
}
public void setSocketTimeout(Integer socketTimeout) {
this.socketTimeout = socketTimeout;
}
public CloseableHttpClient getHttpClient() {
return httpClient;
}
public void setHttpClient(CloseableHttpClient httpClient) {
this.httpClient = httpClient;
}
public CookieStore getCookieStore() {
return cookieStore;
}
public void setCookieStore(CookieStore cookieStore) {
for(Cookie cookie:cookieStore.getCookies()){
this.cookieStore.addCookie(cookie);
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
- 368
- 369
- 370
- 371
- 372
- 373
- 374
- 375
- 376
- 377
- 378
- 379
- 380
- 381
- 382
- 383
- 384
- 385
- 386
- 387
- 388
- 389
- 390
- 391
- 392
- 393
- 394
- 395
- 396
- 397
- 398
- 399
- 400
- 401
- 402
- 403
- 404
- 405
- 406
- 407
- 408
- 409
- 410
- 411
- 412
- 413
- 414
- 415
- 416
- 417
- 418
- 419
- 420
- 421
- 422
- 423
- 424
- 425
- 426
- 427
- 428
- 429
- 430
- 431
- 432
- 433
- 434
- 435
- 436
- 437
- 438
- 439
- 440
- 441
- 442
- 443
- 444
- 445
- 446
- 447
- 448
- 449
- 450
- 451
- 452
- 453
- 454
- 455
- 456
- 457
- 458
- 459
- 460
- 461
- 462
- 463
- 464
- 465
- 466
- 467
- 468
- 469
- 470
- 471
- 472
- 473
- 474
- 475
- 476
- 477
- 478
- 479
- 480
- 481
- 482
- 483
- 484
- 485
- 486
- 487
- 488
- 489
- 490
- 491
- 492
- 493
- 494
- 495
- 496
- 497
- 498
- 499
- 500
- 501
- 502
- 503
- 504
- 505
- 506
- 507
- 508
- 509
- 510
- 511
- 512
- 513
- 514
- 515
- 516
- 517
- 518
- 519
- 520
- 521
- 522
- 523
- 524
- 525
- 526
- 527
- 528
- 529
- 530
- 531
- 532
- 533
- 534
- 535
- 536
- 537
- 538
- 539
- 540
- 541
- 542
- 543
- 544
- 545
- 546
- 547
- 548
- 549
- 550
- 551
- 552
- 553
- 554
- 555
- 556
- 557
- 558
- 559
- 560
- 561
- 562
- 563
- 564
- 565
- 566
- 567
- 568
- 569
- 570
- 571
- 572
- 573
- 574
- 575
- 576
- 577
- 578
- 579
- 580
- 581
- 582
- 583
- 584
- 585
- 586
- 587
- 588
- 589
- 590
- 591
- 592
- 593
- 594
- 595
- 596
- 597
- 598
- 599
- 600
- 601
- 602
- 603
- 604
- 605
- 606
- 607
- 608
- 609
- 610
- 611
- 612
- 613
- 614
我们来看看测试结果
1. 不用代理使用 默认的构造方法,结果如下
将经纬度传到百度LBS云:
从百度地图云获得数据:
{
"status": 0,
"total": 6,
"size": 6,
"contents": [
{
"uid": 1694507525,
"province": "北京市",
"geotable_id": 137287,
"district": "东城区",
"create_time": 1463322154,
"city": "北京市",
"location": [
116.428415,
39.927109
],
"address": "北京",
"title": "武汉纺织大学5号宿舍楼",
"coord_type": 3,
"direction": "附近",
"type": 0,
"distance": 0,
"weight": 0
},
{
"uid": 1694513755,
"province": "北京市",
"geotable_id": 137287,
"district": "东城区",
"create_time": 1463322333,
"city": "北京市",
"location": [
116.428415,
39.927109
],
"address": "北京",
"title": "武汉纺织大学5号宿舍楼",
"coord_type": 3,
"direction": "附近",
"type": 0,
"distance": 0,
"weight": 0
},
{
"uid": 1694514187,
"province": "北京市",
"geotable_id": 137287,
"district": "东城区",
"create_time": 1463322402,
"city": "北京市",
"location": [
116.428415,
39.927109
],
"address": "北京",
"title": "武汉纺织大学5号宿舍楼",
"coord_type": 3,
"direction": "附近",
"type": 0,
"distance": 0,
"weight": 0
},
{
"uid": 1694526535,
"province": "北京市",
"geotable_id": 137287,
"district": "东城区",
"create_time": 1463322534,
"city": "北京市",
"location": [
116.428415,
39.927109
],
"address": "北京",
"title": "武汉纺织大学5号宿舍楼",
"coord_type": 3,
"direction": "附近",
"type": 0,
"distance": 0,
"weight": 0
},
]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
-
用代理,使用含有代理的构造方法,结果如下
使用代理,记得Charles开启,本地代理主机127.0.0.1或者localhost,端口8888(Charles默认),这里只截取charles 的界面,控制台显示是一样的。
将经纬度传到百度LBS云:
- Request 可以看到请求的内容,我们可以看请求地址为http://api.map.baidu.com/geodata/v3/poi/create
- 响应内容为:
从百度地图云获得数据:
- Request :
- Response:
到此,我们就知道如何用HttpClient工具类来请求数据了,后台要负责很多事情,比如网络请求的数据进行解析,这时HttpClient显得就重要了。
这一讲就讲到这里,有不对的地方,菜鸟希望大家指出。
下一讲,我就专门找个网站,模拟请求(登录,并抓取返回的数据)。
最后附上工程源码(Maven 项目): HttpClient