JAVA 에서 HTTPS 를 보 내 는 POST 요청 도구 클래스
6727 단어 네트워크 프로 그래 밍
package com.common.util;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
@SuppressWarnings("deprecation")
public class HttpsUtils
{
/**
* HTTPS POST
* @param reqURL
* @param params
* @return
*/
@SuppressWarnings({ "finally", "resource" })
public static String sendSSLPostRequest(String reqURL, Map params)
{
long responseLength = 0; //
String responseContent = null; //
HttpClient httpClient = new DefaultHttpClient(); // httpClient
X509TrustManager xtm = new X509TrustManager()
{ // TrustManager
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() { return null; }
};
try
{
//TLS1.0 SSL3.0 , TLS SSL , SSLContext
SSLContext ctx = SSLContext.getInstance("TLS");
// TrustManager ,TrustManager SSL Socket
ctx.init(null, new TrustManager[]{xtm}, null);
// SSLSocketFactory
SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
// SchemeRegistry SSLSocketFactory HttpClient
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory));
HttpPost httpPost = new HttpPost(reqURL); // HttpPost
List formParams = new ArrayList(); // POST
if(params != null)
{
for(Map.Entry entry : params.entrySet()){
formParams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost); // POST
HttpEntity entity = response.getEntity(); //
if (null != entity) {
responseLength = entity.getContentLength();
responseContent = EntityUtils.toString(entity, "UTF-8");
EntityUtils.consume(entity); //Consume response content
}
System.out.println(" : " + httpPost.getURI());
System.out.println(" : " + response.getStatusLine());
System.out.println(" : " + responseLength);
System.out.println(" : " + responseContent);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
httpClient.getConnectionManager().shutdown(); // ,
return responseContent;
}
}
@SuppressWarnings({ "finally", "resource" })
public static String sendSSLPostBodyRequest(String reqURL, String bodyStr)
{
long responseLength = 0; //
String responseContent = null; //
HttpClient httpClient = new DefaultHttpClient(); // httpClient
X509TrustManager xtm = new X509TrustManager()
{ // TrustManager
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {}
public X509Certificate[] getAcceptedIssuers() { return null; }
};
try
{
//TLS1.0 SSL3.0 , TLS SSL , SSLContext
SSLContext ctx = SSLContext.getInstance("TLS");
// TrustManager ,TrustManager SSL Socket
ctx.init(null, new TrustManager[]{xtm}, null);
// SSLSocketFactory
SSLSocketFactory socketFactory = new SSLSocketFactory(ctx);
// SchemeRegistry SSLSocketFactory HttpClient
httpClient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, socketFactory));
HttpPost httpPost = new HttpPost(reqURL); // HttpPost
if(!StringUtils.isBlank(bodyStr))
{
StringEntity requestEntity = new StringEntity(bodyStr, "UTF-8");
httpPost.setEntity(requestEntity);
}
HttpResponse response = httpClient.execute(httpPost); // POST
HttpEntity entity = response.getEntity(); //
if (null != entity) {
responseLength = entity.getContentLength();
responseContent = EntityUtils.toString(entity, "UTF-8");
EntityUtils.consume(entity); //Consume response content
}
System.out.println(" : " + httpPost.getURI());
System.out.println(" : " + response.getStatusLine());
System.out.println(" : " + responseLength);
System.out.println(" : " + responseContent);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
httpClient.getConnectionManager().shutdown(); // ,
return responseContent;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 네트워크 프로 그래 밍 -> 상용 모듈스 트림 소켓 을 만 들 고 지정 한 IP 주소 의 지정 한 포트 번호 에 연결 합 니 다. 이 소켓 의 입력 흐름 을 되 돌려 줍 니 다. 이 소켓 의 출력 흐름 을 되 돌려 줍 니 다. 이 IP 주소 의 호스트 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.