天天看點

java模拟https請求_java-用httpclient模拟發送https請求

--調用項目提供接口,接口是用HTTP URL實作的,最初的想法是另一個項目用jQuery

post進行請求。

---但是,很可能另一個項目是部署在别的機器上,那麼就存在跨域問題,而jquery的post請求是不允許跨域的。

---這時,就隻能夠用HttpClient包進行請求了,同時由于請求的URL是HTTPS的,為了避免需要證書,是以用一-----個類繼承DefaultHttpClient類,忽略校驗過程。

1.https是需要ssl證書,即使是沒用的證書也需要建立.

public 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);

return

HttpClients.custom().setSSLSocketFactory(sslsf).build();

} catch (KeyManagementException e)

{

e.printStackTrace();

} catch (NoSuchAlgorithmExceptione)

{

e.printStackTrace();

} catch (KeyStoreException e) {

e.printStackTrace();

}

return

HttpClients.createDefault();

}

public static String

doPost(String url, String requestXML, String token, String

signatrue, String charset,

boolean pretty, String contentType) {

HttpPost httpPost = new

HttpPost(url);

String resp = null;

try {

if (StringUtils.isNotBlank(token)) {

httpPost.setHeader("4GGOGO-Auth-Token",

token);

}

if (StringUtils.isNotBlank(signatrue))

{

httpPost.setHeader("HTTP-X-4GGOGO-Signature",

signatrue);

}

if (StringUtils.isNotBlank(contentType))

{

httpPost.setHeader("Content-Type",

contentType);

}

httpPost.setEntity(new

StringEntity(requestXML,"utf-8"));

CloseableHttpClient httpClient =

createSSLClientDefault();

HttpResponse httpResponse =

httpClient.execute(httpPost);

HttpEntity entity =

httpResponse.getEntity();

if (entity != null) {

//按指定編碼轉換結果實體為String類型

resp =

EntityUtils.toString(entity, "UTF-8");

}

} catch

(ClientProtocolException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return resp;

}

public

static String doGet(String url, String queryString, String token,

String signatrue, String charset,

boolean pretty, String contentType) {

StringBuffer response = new

StringBuffer();

HttpClient client = new

HttpClient();

HttpMethod method = new

GetMethod(url);

try {

if (StringUtils.isNotBlank(queryString))

{

method.setQueryString(URIUtil.encodeQuery(queryString));

}

if (StringUtils.isNotBlank(signatrue))

{

method.addRequestHeader("HTTP-X-4GGOGO-Signature",

signatrue);

}

if (StringUtils.isNotBlank(token)) {

method.addRequestHeader("4GGOGO-Auth-Token", token);

}

method.setRequestHeader("content-type",

contentType);

client.executeMethod(method);

System.out.println("傳回的狀态碼為" +

method.getStatusCode());

if (method.getStatusCode() == HttpStatus.SC_OK)

{

BufferedReader reader = new BufferedReader(new

InputStreamReader(method.getResponseBodyAsStream(),

charset));

String

line;

while

((line = reader.readLine()) != null) {

if (pretty) {

response.append(line).append(System.getProperty("line.separator"));

} else {

response.append(line);

}

}

reader.close();

}

} catch (URIException e)

{

System.out.println("執行HTTP Get請求時,編碼查詢字元串'" +

queryString + "'發生異常!" + e.getMessage());

} catch (IOException e)

{

System.out.println("執行HTTP Get請求時,編碼查詢字元串'" +

queryString + "'發生異常!" + e.getMessage());

} finally {

method.releaseConnection();

}

return

response.toString();

}

public

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);

return

HttpClients.custom().setSSLSocketFactory(sslsf).build();

} catch

(KeyManagementException e) {

e.printStackTrace();

} catch

(NoSuchAlgorithmExceptione) {

e.printStackTrace();

} catch

(KeyStoreException e) {

e.printStackTrace();

}

return

HttpClients.createDefault();

}