「Java開発」Javaでhttpsクライアント、サーバー側処理サンプルプログラム
1.クラスの定義
rivate static class MySSLSocketFactory extends SSLSocketFactory {
SSLContext sslContext = SSLContext.getInstance(“TLS");
public MySSLSocketFactory(KeyStore truststore)
throws NoSuchAlgorithmException, KeyManagementException,
KeyStoreException, UnrecoverableKeyException {
super(truststore);
TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
sslContext.init(null, new TrustManager[] { tm }, null);
}
@Override
public Socket createSocket(Socket socket, String host, int port,
boolean autoClose) throws IOException, UnknownHostException {
return sslContext.getSocketFactory().createSocket(socket, host,
port, autoClose);
}
@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
}
2.HttpClient側処理プログラム
private static HttpClient getNewHttpClient() {
try {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
trustStore.load(null, null);
SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme(“http", PlainSocketFactory
.getSocketFactory(), 80));
registry.register(new Scheme(“https", sf, 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(
params, registry);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,SET_SOCKET_TIMEOUT);
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,SET_CONNECTION_TIMEOUT);
return client;
} catch (Exception e) {
return new DefaultHttpClient();
}
}
3.サーバーリクエスト
public static JSONObject getAccessTokenByPost(String url, JSONObject pa) {
HttpClient httpClient = getNewHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(“Content-Type", “application/x-www-form-urlencoded");
httpPost.setHeader(“authorization", “Basic dG91Y2hhYXBwOnNlY3JldHBhc3N3b3Jk");
httpPost.setHeader(“connection", “Keep-Alive");
httpPost.setHeader(“accept", “*/*");
JSONObject result = null;
try {
if (pa != null) {
Map<String, String> map = (Map<String, String>) pa.get(“value");
String parm = getRequestData(map);
httpPost.setEntity(new StringEntity(parm));
}
// httpPostフォームの送信
HttpResponse response = httpClient.execute(httpPost);
//サーバーの応答エンティティオブジェクトを取得
HttpEntity responseEntity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
String body = EntityUtils.toString(responseEntity, “utf-8″);
if (!"".equals(body)&& body !=null ) {
result = new JSONObject(body);
}else {
result = new JSONObject();
}
result.put(“statusCode", HttpStatus.SC_OK);
} else {
result = new JSONObject();
result.put(“statusCode", statusCode);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// リソースを解放
httpClient.getConnectionManager().shutdown();
}
return result;
}