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

httpClient token认证 接口调用示例

时间:2018-12-28 18:29:34      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:page   security   and   uri   new t   test   charset   bool   content   

package com.bbd.onet.data.service.impl;


import com.github.jsonldjava.utils.JsonUtils;
import org.apache.http.conn.ssl.AllowAllHostnameVerifier;
import org.apache.http.conn.ssl.SSLSocketFactory;

import javax.net.ssl.*;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

public class DataOperatorServiceImplTest {

public static void main(String[] args) throws Exception {
String params = "{\"modelId\":\"737c4ea4c120458d8ea8f9e825731490\",\"name\":\"原告\",\"type\":0,\"pageNo\":1,\"pageSize\":10}";
HttpURLConnection connection = connectToWeb("http://221.226.218.150:8766/api/data/queryData", "POST");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
writer.write(params);
writer.close();
int result = connection.getResponseCode();
if (result == 200) {
Object object = getResult(connection.getInputStream());
System.out.println(JsonUtils.toString(object));
} else {
System.out.println(".........fail..........");
}
connection.disconnect();
}

public static String getResult(InputStream inputStream) {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int len = 0;
String result = null;
if (inputStream != null) {
try {
while ((len = inputStream.read(data)) != -1) {
bo.write(data, 0, len);
}
result = new String(bo.toByteArray(), "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}

public static HttpURLConnection connectToWeb(String uri, String method) {
SSLSocketFactory.getSocketFactory().setHostnameVerifier(new AllowAllHostnameVerifier());
try {
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}}, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
} catch (Exception e) {
e.printStackTrace();
}

HttpURLConnection connection = null;
try {
URL url = new URL(uri);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "bearer 1613c88e-812e-4567-a65a-29d87d2d59d9");
connection.setRequestProperty("Content-type", "application/json;charset=UTF-8");
connection.setRequestMethod(method);
connection.setDoOutput(true);
connection.connect();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return connection;
}
}

httpClient token认证 接口调用示例

标签:page   security   and   uri   new t   test   charset   bool   content   

原文地址:https://www.cnblogs.com/wg1234/p/10192063.html

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