Hprose 是PHPrpc的升级版本,支持Https调用;
下面给出Android 调用的方式和 Hprose-java的下载地址。
/**
* hprose
*
* @return
* @throws IOException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static String hprose(String jsonString) throws IOException {
HproseHttpClient client = new HproseHttpClient();
NetHelper(client);
client.useService("https://192.168.0.250:8443/Hello");
String result = (String) client.invoke("sayHello",
new Object[] { "Hprose" });
Log.d("HPROSE", "resut: " + result);
return result;
}
private static void NetHelper(HproseHttpClient client) {
X509HostnameVerifier hostnameVerifier = SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
SSLContext sslContext = null;
try {
MyX509TrustManager mtm = new MyX509TrustManager();
TrustManager[] tms = new TrustManager[] { mtm };
// 初始化X509TrustManager中的SSLContext
sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tms, new java.security.SecureRandom());
} catch (Exception e) {
e.printStackTrace();
}
// 为javax.net.ssl.HttpsURLConnection设置默认的SocketFactory和HostnameVerifier
if (sslContext != null) {
// client.setDefaultSSLSocketFactory(sslContext
// .getSocketFactory());
client.setSSLSocketFactory(sslContext.getSocketFactory());
}
// client.setDefaultHostnameVerifier(hostnameVerifier);
client.setHostnameVerifier(hostnameVerifier);
}
public static class MyX509TrustManager implements X509TrustManager {
X509TrustManager myJSSEX509TrustManager = null;
public MyX509TrustManager() throws Exception {
KeyStore ks = KeyStore.getInstance("BKS");
// ks.load(new FileInputStream("trustedCerts"),
// "passphrase".toCharArray()); //---->
// 这是加载自己的数字签名证书文件和密码,在这里这里没有,所以不需要
TrustManagerFactory tmf = TrustManagerFactory.getInstance("X509");
tmf.init(ks);
TrustManager tms[] = tmf.getTrustManagers();
for (int i = 0; i < tms.length; i++) {
if (tms[i] instanceof X509TrustManager) {
myJSSEX509TrustManager = (X509TrustManager) tms[i];
return;
}
}
}
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// sunJSSEX509TrustManager.checkClientTrusted(arg0, arg1);
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1)
throws CertificateException {
// sunJSSEX509TrustManager.checkServerTrusted(arg0, arg1);
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// X509Certificate[] acceptedIssuers = sunJSSEX509TrustManager
// .getAcceptedIssuers();
// return acceptedIssuers;
return null;
}
}
Hprose-java下载地址:https://github.com/hprose/hprose-java