Java tutorial
/* * Copyright 2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package cn.cuizuoli.appranking.http.client; import java.io.IOException; import java.net.HttpURLConnection; import java.net.Proxy; import java.net.URL; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import org.apache.commons.lang.StringUtils; import org.springframework.http.client.SimpleClientHttpRequestFactory; /** * TrustClientHttpRequestFactory * @author cuizuoli */ public class TrustClientHttpRequestFactory extends SimpleClientHttpRequestFactory { private SSLSocketFactory sslSocketFactory; private final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }; @Override protected HttpURLConnection openConnection(URL url, Proxy proxy) throws IOException { if (StringUtils.equals(url.getProtocol(), "https")) { HttpsURLConnection.setDefaultSSLSocketFactory(sslSocketFactory); HttpsURLConnection urlConnection = (HttpsURLConnection) (proxy != null ? url.openConnection(proxy) : url.openConnection()); urlConnection.setHostnameVerifier(DO_NOT_VERIFY); return urlConnection; } else { HttpURLConnection urlConnection = (HttpURLConnection) (proxy != null ? url.openConnection(proxy) : url.openConnection()); return urlConnection; } } public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) { this.sslSocketFactory = sslSocketFactory; } }