//rest接口访问地址特征
API_IP = http://localhost:8080/mano-vim
HttpClientRequest requestProvider = new HttpClientGetRequest(ConfigFileLoad.getConfContent("API_IP") + "/rest/provider_network_cn");
@Component
@Path("/")
public class ProvidernetRest {
@GET
@Path("provider_network_cn")
@Produces(ContentType.APPLICATION_JSON_UTF_8)
@Consumes(MediaType.TEXT_PLAIN)
public Object findProvidercnCount(@HeaderParam("X-Auth-Token") String token)
{
}
}
http://127.0.0.1:8080/mano-vim/rest(这个是要的)/provider_network_cn
客户端:
@Override
public String findProvidercnCount(String token) throws Exception {
try {
String url = scapeRestRootUrl + "/token_api/ems_exist";
if ((null != token) && (!token.trim().equalsIgnoreCase(""))) {
url = url + "?X-Auth-Token=" + token;
}
String result = new String(HttpsUtil.getMethod(url));
return result;
} catch (Exception ex) {
logger.error("ex:" + ex);
throw ex;
}
};
工具类:
package com.certusnet.nfv.mano.vim;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
public class HttpsUtil {
/**
* 忽视证书HostName
*/
private static HostnameVerifier ignoreHostnameVerifier = new HostnameVerifier() {
public boolean verify(String s, SSLSession sslsession) {
System.out.println("WARNING: Hostname is not matched for cert.");
return true;
}
};
/**
* Ignore Certification
*/
private static TrustManager ignoreCertificationTrustManger = new X509TrustManager() {
private X509Certificate[] certificates;
@Override
public void checkClientTrusted(X509Certificate certificates[],
String authType) throws CertificateException {
if (this.certificates == null) {
this.certificates = certificates;
System.out.println("init at checkClientTrusted");
}
}
@Override
public void checkServerTrusted(X509Certificate[] ax509certificate,
String s) throws CertificateException {
if (this.certificates == null) {
this.certificates = ax509certificate;
System.out.println("init at checkServerTrusted");
}
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
};
public static String getMethod(String urlString) {
ByteArrayOutputStream buffer = new ByteArrayOutputStream(512);
try {
URL url = new URL(urlString);
/*
* use ignore host name verifier
*/
HttpsURLConnection.setDefaultHostnameVerifier(ignoreHostnameVerifier);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
// Prepare SSL Context
TrustManager[] tm = { ignoreCertificationTrustManger };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
connection.setSSLSocketFactory(ssf);
InputStream reader = connection.getInputStream();
byte[] bytes = new byte[512];
int length = reader.read(bytes);
do {
buffer.write(bytes, 0, length);
length = reader.read(bytes);
} while (length > 0);
// result.setResponseData(bytes);
System.out.println(buffer.toString());
reader.close();
connection.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
}
String repString= new String (buffer.toByteArray());
return repString;
}
public static void main(String[] args) {
String urlString = "https://172.16.67.192/vm_api/list";
String output = new String(HttpsUtil.getMethod(urlString));
//System.out.println(output);
}
}
rest 应用demo 3 客户端的另一中方式
猜你喜欢
转载自yuhuiblog6338999322098842.iteye.com/blog/2207628
今日推荐
周排行