码迷,mamicode.com
首页 > Web开发 > 详细

HTTP请求(CloseableHttpClient是否就是HTTP协议规则的实现?)

时间:2018-01-21 20:39:08      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:ssi   log   cto   设定   conf   开发   http post   cer   tpc   

发起HTTP协议的交互请求:
想发起就能发起(符合万维网系统制定的HTTP协议即可,规定格式的请求头,请求体等),但能不能返回和返回什么由目标接口决定。如果返回有问题,生成的状态码是万维网系统检测并返回吗?为什么后台开发者也能设定状态码?

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class HttpClientUtil {

/**
* 发送http get 请求
* @param url url
* @return 响应体字符串
* @throws IOException IOException
*/
public static String httpGet(String url) throws IOException {

String responseStr;

CloseableHttpClient httpClient = createSSLClientDefault();

HttpGet httpget = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(45000).build();
httpget.setConfig(requestConfig);

ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

@Override
public String handleResponse(final HttpResponse response) throws IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected message status: " + status);
}
}

};

responseStr = httpClient.execute(httpget, responseHandler);

return responseStr;
}

/**
* 发送http post 请求
* @param url url
* @return 响应体字符串
* @throws IOException IOException
*/
public static String httpPost(String url, String body) throws IOException {

String responseStr;

CloseableHttpClient httpClient = createSSLClientDefault();

HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/json");

RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(45000).build();
httpPost.setConfig(requestConfig);

HttpEntity entity = new ByteArrayEntity(body.getBytes("utf-8"));
httpPost.setEntity(entity);

ResponseHandler<String> responseHandler = new ResponseHandler<String>() {

@Override
public String handleResponse(final HttpResponse response) throws IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= 200 && status < 300) {
HttpEntity entity = response.getEntity();
return entity != null ? EntityUtils.toString(entity) : null;
} else {
throw new ClientProtocolException("Unexpected message status: " + status);
}
}

};

responseStr = httpClient.execute(httpPost, responseHandler);

return responseStr;
}

/**
* 创建可以发送https请求的httpclient
* @return CloseableHttpClient
*/
private static CloseableHttpClient createSSLClientDefault() {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new MyHostnameVerifier());
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
e.printStackTrace();
}
return HttpClients.createDefault();
}

// SSLPeerUnverifiedException: Host name err
private static class MyHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}

}

}


HTTP请求(CloseableHttpClient是否就是HTTP协议规则的实现?)

标签:ssi   log   cto   设定   conf   开发   http post   cer   tpc   

原文地址:https://www.cnblogs.com/jianmianruxin/p/8325066.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!