httpClient token认证 接口调用示例

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

猜你喜欢

转载自www.cnblogs.com/wg1234/p/10192063.html