本文整理汇总了Java中javax.net.ssl.HttpsURLConnection.setHostnameVerifier方法的典型用法代码示例。如果您正苦于以下问题:Java HttpsURLConnection.setHostnameVerifier方法的具体用法?Java HttpsURLConnection.setHostnameVerifier怎么用?Java HttpsURLConnection.setHostnameVerifier使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.net.ssl.HttpsURLConnection
的用法示例。
在下文中一共展示了HttpsURLConnection.setHostnameVerifier方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: createConnectionGet
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 创建连接
*
* @return
* @throws ProtocolException
*/
private HttpURLConnection createConnectionGet(String encoding) throws ProtocolException {
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
LogUtil.writeErrorLog(e.getMessage(), e);
return null;
}
httpURLConnection.setConnectTimeout(this.connectionTimeout);// 连接超时时间
httpURLConnection.setReadTimeout(this.readTimeOut);// 读取结果超时时间
httpURLConnection.setUseCaches(false);// 取消缓存
httpURLConnection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded;charset=" + encoding);
httpURLConnection.setRequestMethod("GET");
if ("https".equalsIgnoreCase(url.getProtocol())) {
HttpsURLConnection husn = (HttpsURLConnection) httpURLConnection;
//是否验证https证书,测试环境请设置false,生产环境建议优先尝试true,不行再false
if(!SDKConfig.getConfig().isIfValidateRemoteCert()){
husn.setSSLSocketFactory(new BaseHttpSSLSocketFactory());
husn.setHostnameVerifier(new BaseHttpSSLSocketFactory.TrustAnyHostnameVerifier());//解决由于服务器证书问题导致HTTPS无法访问的情况
}
return husn;
}
return httpURLConnection;
}
示例2: varyAndHttps
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
@Test public void varyAndHttps() throws Exception {
assumeFalse(getPlatform().equals("jdk9"));
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().addHeader("Cache-Control: max-age=60")
.addHeader("Vary: Accept-Language")
.setBody("A"));
server.enqueue(new MockResponse().setBody("B"));
URL url = server.url("/").url();
HttpsURLConnection connection1 = (HttpsURLConnection) urlFactory.open(url);
connection1.setSSLSocketFactory(sslClient.socketFactory);
connection1.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
connection1.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection1));
HttpsURLConnection connection2 = (HttpsURLConnection) urlFactory.open(url);
connection2.setSSLSocketFactory(sslClient.socketFactory);
connection2.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
connection2.addRequestProperty("Accept-Language", "en-US");
assertEquals("A", readAscii(connection2));
}
示例3: secureResponseCaching
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
@Test public void secureResponseCaching() throws IOException {
assumeFalse(getPlatform().equals("jdk9"));
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.setBody("ABC"));
HttpsURLConnection c1 = (HttpsURLConnection) urlFactory.open(server.url("/").url());
c1.setSSLSocketFactory(sslClient.socketFactory);
c1.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
assertEquals("ABC", readAscii(c1));
// OpenJDK 6 fails on this line, complaining that the connection isn't open yet
String suite = c1.getCipherSuite();
List<Certificate> localCerts = toListOrNull(c1.getLocalCertificates());
List<Certificate> serverCerts = toListOrNull(c1.getServerCertificates());
Principal peerPrincipal = c1.getPeerPrincipal();
Principal localPrincipal = c1.getLocalPrincipal();
HttpsURLConnection c2 = (HttpsURLConnection) urlFactory.open(server.url("/").url()); // cached!
c2.setSSLSocketFactory(sslClient.socketFactory);
c2.setHostnameVerifier(NULL_HOSTNAME_VERIFIER);
assertEquals("ABC", readAscii(c2));
assertEquals(2, cache.requestCount());
assertEquals(1, cache.networkCount());
assertEquals(1, cache.hitCount());
assertEquals(suite, c2.getCipherSuite());
assertEquals(localCerts, toListOrNull(c2.getLocalCertificates()));
assertEquals(serverCerts, toListOrNull(c2.getServerCertificates()));
assertEquals(peerPrincipal, c2.getPeerPrincipal());
assertEquals(localPrincipal, c2.getLocalPrincipal());
}
示例4: createConnection
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 创建连接
*
* @return
* @throws ProtocolException
*/
private HttpURLConnection createConnection(String encoding) throws ProtocolException {
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
LogUtil.writeErrorLog(e.getMessage(), e);
return null;
}
httpURLConnection.setConnectTimeout(this.connectionTimeout);// 连接超时时间
httpURLConnection.setReadTimeout(this.readTimeOut);// 读取结果超时时间
httpURLConnection.setDoInput(true); // 可读
httpURLConnection.setDoOutput(true); // 可写
httpURLConnection.setUseCaches(false);// 取消缓存
httpURLConnection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded;charset=" + encoding);
httpURLConnection.setRequestMethod("POST");
if ("https".equalsIgnoreCase(url.getProtocol())) {
HttpsURLConnection husn = (HttpsURLConnection) httpURLConnection;
husn.setSSLSocketFactory(new BaseHttpSSLSocketFactory());
husn.setHostnameVerifier(new TrustAnyHostnameVerifier());//解决由于服务器证书问题导致HTTPS无法访问的情况
return husn;
}
return httpURLConnection;
}
示例5: createConnectionGet
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 创建连接
*
* @return
* @throws ProtocolException
*/
private HttpURLConnection createConnectionGet(String encoding) throws ProtocolException {
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
LogUtil.writeErrorLog(e.getMessage(), e);
return null;
}
httpURLConnection.setConnectTimeout(this.connectionTimeout);// 连接超时时间
httpURLConnection.setReadTimeout(this.readTimeOut);// 读取结果超时时间
httpURLConnection.setUseCaches(false);// 取消缓存
httpURLConnection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded;charset=" + encoding);
httpURLConnection.setRequestMethod("GET");
if ("https".equalsIgnoreCase(url.getProtocol())) {
HttpsURLConnection husn = (HttpsURLConnection) httpURLConnection;
husn.setSSLSocketFactory(new BaseHttpSSLSocketFactory());
husn.setHostnameVerifier(new TrustAnyHostnameVerifier());//解决由于服务器证书问题导致HTTPS无法访问的情况
return husn;
}
return httpURLConnection;
}
示例6: cacheReturnsInsecureResponseForSecureRequest
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
@Test public void cacheReturnsInsecureResponseForSecureRequest() throws IOException {
assumeFalse(getPlatform().equals("jdk9"));
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse().setBody("ABC"));
server.enqueue(new MockResponse().setBody("DEF"));
AndroidInternal.setResponseCache(urlFactory, new InsecureResponseCache(cache));
HttpsURLConnection connection1 = (HttpsURLConnection) openConnection(server.url("/").url());
connection1.setSSLSocketFactory(sslClient.socketFactory);
connection1.setHostnameVerifier(hostnameVerifier);
assertEquals("ABC", readAscii(connection1));
// Not cached!
HttpsURLConnection connection2 = (HttpsURLConnection) openConnection(server.url("/").url());
connection2.setSSLSocketFactory(sslClient.socketFactory);
connection2.setHostnameVerifier(hostnameVerifier);
assertEquals("DEF", readAscii(connection2));
}
示例7: doClient
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
void doClient() throws IOException {
InetSocketAddress address = httpsServer.getAddress();
URL url = new URL("https://localhost:" + address.getPort() + "/test6614957/");
System.out.println("trying to connect to " + url + "...");
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
SimpleSSLSocketFactory sssf = new SimpleSSLSocketFactory();
uc.setSSLSocketFactory(sssf);
uc.setHostnameVerifier(new AllHostnameVerifier());
InputStream is = uc.getInputStream();
byte[] ba = new byte[1024];
int read = 0;
while ((read = is.read(ba)) != -1) {
System.out.println(new String(ba, 0, read));
}
System.out.println("SimpleSSLSocketFactory.socketCreated = " + sssf.socketCreated);
System.out.println("SimpleSSLSocketFactory.socketWrapped = " + sssf.socketWrapped);
if (!sssf.socketCreated)
throw new RuntimeException("Failed: Socket Factory not being called to create Socket");
}
示例8: doClient
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
void doClient() throws IOException {
InetSocketAddress address = httpsServer.getAddress();
URL url = new URL("https://localhost:" + address.getPort() + "/");
System.out.println("trying to connect to " + url + "...");
HttpsURLConnection uc = (HttpsURLConnection) url.openConnection();
uc.setHostnameVerifier(new AllHostnameVerifier());
if (uc instanceof javax.net.ssl.HttpsURLConnection) {
((javax.net.ssl.HttpsURLConnection) uc).setSSLSocketFactory(new SimpleSSLSocketFactory());
System.out.println("Using TestSocketFactory");
}
uc.connect();
System.out.println("CONNECTED " + uc);
System.out.println(uc.getResponseMessage());
uc.disconnect();
}
示例9: secureResponseCaching
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
@Test public void secureResponseCaching() throws IOException {
assumeFalse(getPlatform().equals("jdk9"));
server.useHttps(sslClient.socketFactory, false);
server.enqueue(new MockResponse()
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.setBody("ABC"));
HttpsURLConnection c1 = (HttpsURLConnection) openConnection(server.url("/").url());
c1.setSSLSocketFactory(sslClient.socketFactory);
c1.setHostnameVerifier(hostnameVerifier);
assertEquals("ABC", readAscii(c1));
// OpenJDK 6 fails on this line, complaining that the connection isn't open yet
String suite = c1.getCipherSuite();
List<Certificate> localCerts = toListOrNull(c1.getLocalCertificates());
List<Certificate> serverCerts = toListOrNull(c1.getServerCertificates());
Principal peerPrincipal = c1.getPeerPrincipal();
Principal localPrincipal = c1.getLocalPrincipal();
HttpsURLConnection c2 = (HttpsURLConnection) openConnection(server.url("/").url()); // cached!
c2.setSSLSocketFactory(sslClient.socketFactory);
c2.setHostnameVerifier(hostnameVerifier);
assertEquals("ABC", readAscii(c2));
assertEquals(suite, c2.getCipherSuite());
assertEquals(localCerts, toListOrNull(c2.getLocalCertificates()));
assertEquals(serverCerts, toListOrNull(c2.getServerCertificates()));
assertEquals(peerPrincipal, c2.getPeerPrincipal());
assertEquals(localPrincipal, c2.getLocalPrincipal());
}
示例10: get
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 鍙戦�丟et璇锋眰
* @param url
* @return
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws IOException
* @throws KeyManagementException
*/
public static String get(String url,Boolean https) throws NoSuchAlgorithmException, NoSuchProviderException, IOException, KeyManagementException {
StringBuffer bufferRes = null;
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 浠庝笂杩癝SLContext瀵硅薄涓緱鍒癝SLSocketFactory瀵硅薄
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL urlGet = new URL(url);
HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();
// 杩炴帴瓒呮椂
http.setConnectTimeout(25000);
// 璇诲彇瓒呮椂 --鏈嶅姟鍣ㄥ搷搴旀瘮杈冩參锛屽澶ф椂闂�
http.setReadTimeout(25000);
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setSSLSocketFactory(ssf);
http.setHostnameVerifier(new Verifier());
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null){
bufferRes.append(valueString);
}
in.close();
if (http != null) {
// 鍏抽棴杩炴帴
http.disconnect();
}
return bufferRes.toString();
}
示例11: configureConnection
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
private HttpURLConnection configureConnection(HttpURLConnection conn)
throws IOException {
if (sslFactory != null) {
HttpsURLConnection httpsConn = (HttpsURLConnection) conn;
try {
httpsConn.setSSLSocketFactory(sslFactory.createSSLSocketFactory());
} catch (GeneralSecurityException ex) {
throw new IOException(ex);
}
httpsConn.setHostnameVerifier(sslFactory.getHostnameVerifier());
}
return conn;
}
示例12: buildSSLConn
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
public HttpURLConnection buildSSLConn(String url)throws Exception {
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[]{new TrustAnyTrustManager()}, new java.security.SecureRandom());
URL console = new URL(url);
HttpsURLConnection conn = (HttpsURLConnection) console.openConnection();
conn.setSSLSocketFactory(sc.getSocketFactory());
conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
conn.setConnectTimeout(connectTimeOut);
conn.setReadTimeout(readTimeOut);
return conn;
}
示例13: createConnection
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 创建连接
*
* @return
* @throws ProtocolException
*/
private HttpURLConnection createConnection(String encoding) throws ProtocolException {
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
LogUtil.writeErrorLog(e.getMessage(), e);
return null;
}
httpURLConnection.setConnectTimeout(this.connectionTimeout);// 连接超时时间
httpURLConnection.setReadTimeout(this.readTimeOut);// 读取结果超时时间
httpURLConnection.setDoInput(true); // 可读
httpURLConnection.setDoOutput(true); // 可写
httpURLConnection.setUseCaches(false);// 取消缓存
httpURLConnection.setRequestProperty("Content-type",
"application/x-www-form-urlencoded;charset=" + encoding);
httpURLConnection.setRequestMethod("POST");
if ("https".equalsIgnoreCase(url.getProtocol())) {
HttpsURLConnection husn = (HttpsURLConnection) httpURLConnection;
//是否验证https证书,测试环境请设置false,生产环境建议优先尝试true,不行再false
if(!SDKConfig.getConfig().isIfValidateRemoteCert()){
husn.setSSLSocketFactory(new BaseHttpSSLSocketFactory());
husn.setHostnameVerifier(new BaseHttpSSLSocketFactory.TrustAnyHostnameVerifier());//解决由于服务器证书问题导致HTTPS无法访问的情况
}
return husn;
}
return httpURLConnection;
}
示例14: initHttpsConnection
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 针对 https 连接配置
*
* @param conn 连接
* @throws Exception e
*/
private void initHttpsConnection(HttpURLConnection conn) throws Exception {
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) conn;
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[]{new MyTrustManager()}, new SecureRandom());
httpsURLConnection.setSSLSocketFactory(sc.getSocketFactory());
httpsURLConnection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return hostname != null;
}
});
}
示例15: post
import javax.net.ssl.HttpsURLConnection; //导入方法依赖的package包/类
/**
* 鍙戦�丳ost璇锋眰
* @param url
* @param params
* @return
* @throws IOException
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
* @throws KeyManagementException
*/
public static String post(String url, String params,Boolean https) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, KeyManagementException {
StringBuffer bufferRes = null;
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 浠庝笂杩癝SLContext瀵硅薄涓緱鍒癝SLSocketFactory瀵硅薄
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL urlGet = new URL(url);
HttpsURLConnection http = (HttpsURLConnection) urlGet.openConnection();
// 杩炴帴瓒呮椂
http.setConnectTimeout(50000);
// 璇诲彇瓒呮椂 --鏈嶅姟鍣ㄥ搷搴旀瘮杈冩參锛屽澶ф椂闂�
http.setReadTimeout(50000);
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
http.setSSLSocketFactory(ssf);
http.setHostnameVerifier(new Verifier());
http.setDoOutput(true);
http.setDoInput(true);
http.connect();
OutputStream out = http.getOutputStream();
out.write(params.getBytes("UTF-8"));
out.flush();
out.close();
InputStream in = http.getInputStream();
BufferedReader read = new BufferedReader(new InputStreamReader(in, DEFAULT_CHARSET));
String valueString = null;
bufferRes = new StringBuffer();
while ((valueString = read.readLine()) != null){
bufferRes.append(valueString);
}
in.close();
if (http != null) {
// 鍏抽棴杩炴帴
http.disconnect();
}
return bufferRes.toString();
}