java - httpclient - HTTPS 요청
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import com.weixin.information.Config;
public class HttpUtil implements Http {
public InputStream httpsPost(String url, String content, String charset) {
return https(url, content, null, charset, "post");
}
public InputStream httpsPost(String url, Map content,
String charset) {
return https(url, null, content, charset, "post");
}
public InputStream httpsGet(String url) {
return https(url, null, null, null, "get");
}
/**
* https
*
* @param url
* @param content
* @param mapContent
* @param charset
* @param method
* @return
*/
private static InputStream https(String url, String content,
Map mapContent, String charset, String method) {
try {
CloseableHttpClient httpclient = getHttpClient();
CloseableHttpResponse response = null;
InputStream is = null;
try {
// post get
if (method.equals("post")) {
HttpPost httpPost = new HttpPost(url);
// post String Map
if (content != null) {
HttpEntity httpEntity = new StringEntity(content,
charset);
httpPost.setEntity(httpEntity);
} else if (mapContent != null) {
List list = new ArrayList();
for (Map.Entry entry : mapContent
.entrySet()) {
list.add(new BasicNameValuePair(entry.getKey(),
entry.getValue()));
}
httpPost.setEntity(new UrlEncodedFormEntity(list,
charset));
}
response = httpclient.execute(httpPost);
} else if (method.equals("get")) {
HttpGet httpget = new HttpGet(url);
// response
response = httpclient.execute(httpget);
}
// response
HttpEntity entity = response.getEntity();
//
is = entity != null ? entity.getContent() : null;
// entity
EntityUtils.consume(entity);
return is;
} finally {
httpclient.close();
response.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* HttpClient
*
* @return CloseableHttpClient ,
*/
private static CloseableHttpClient getHttpClient() {
try {
FileInputStream fis = null;
CloseableHttpClient httpclient = null;
try {
// PKCS12
KeyStore keyStore = KeyStore.getInstance("PKCS12");
// ( )
File file = new File(Config.getLicense_path());
fis = new FileInputStream(file);
// PKCS12 ( ID)
keyStore.load(fis, Config.getMch_id().toCharArray());
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts
.custom()
.loadKeyMaterial(keyStore,
Config.getMch_id().toCharArray()).build();
// TLS
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslcontext,
new String[] { "TLSv1" },
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
// httpclient SSLSocketFactory
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
return httpclient;
} finally {
fis.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【eclipse】같은 파일을 2개 열고 싶다【에디터의 분할】「이런 것은 다른 클래스로 나누어야 한다!」라든지 있다고는 생각합니다만. 실제로 실무 속에서 프로그램을 쓰고 있으면, 이런 소스에 눈에 걸리는 일도 적지 않을까···. 그건 그렇고, 내 노트북에서 이렇게 보입니다 네...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.