rest 应用demo 3 客户端的另一中方式
来源:互联网 发布:交换机网络端口设置 编辑:程序博客网 时间:2024/06/03 17:49
//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);
}
}
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);
}
}
0 0
- rest 应用demo 3 客户端的另一中方式
- rest 应用demo
- rest 应用demo 2
- rest 两种客户端数据返回的处理方式
- spring 3.0.5 release 对REST 客户端的应用例子
- SpringMVC 中对应的REST的四种请求方式
- 在现有SSH项目中提供REST方式的WebService
- Unity 中服务端与客户端的的代码Demo
- Unity 中服务端与客户端的的代码Demo
- 在客户端程序中使用REST SOE
- 个人应用Demo搭建以及客户端安装
- 基于JavaScript的REST客户端框架
- javascript 实现的CouchDB REST客户端接口
- 我的第一个REST客户端程序!
- python REST API 客户端的 URI 生成器
- 基于REST的Web服务器客户端
- PhpStorm内置的REST客户端 使用方法
- WCF中REST使用POST方式
- 函数调用基础概念原理
- rest 应用demo 2
- 【转】在VS2010中使用Git管理源代码
- Robberies
- Redis快速入门
- rest 应用demo 3 客户端的另一中方式
- 【原创】Android去除标题栏和状态栏
- 【原创】二叉树的中序遍历
- 自定义异常(Exception)和运行时异常(RuntimeException )
- 相比ajax setup 监听弹框另一中页面简单弹框
- 【转】为什么你应该(从现在开始就)写博客
- C与C++的区别
- maven 项目之间的依赖
- mysql 连接报错is blocked because of many connection errors; 解决方法