【发布时间】:2011-06-19 10:08:57
【问题描述】:
这是我的问题/问题:
我正在向通过 https 通过 SSL 进行身份验证的服务器发出 URL 请求,但是当我将输入转换为字符串时,得到答案时总是返回空。 另一个不带参数的调用总是运行顺利
有什么想法吗?
网址:https://www.server.com/api/getResources.php?res[]=Argentina&res[]=Australia&res[]=Bolivia
还有代码:
HttpURLConnection http = conectar(URL);
InputStream urlInputStream = null;
try {
urlInputStream = http.getInputStream();
} catch (IOException e) {
}
连接法:
private HttpURLConnection conectar(String surl) {
URL url = null;
try {
url = new URL(surl);
} catch (MalformedURLException e) {
}
HttpURLConnection http = null;
if (url.getProtocol().toLowerCase().equals("https")) {
trustEveryone();
HttpsURLConnection https = null;
try {
https = (HttpsURLConnection) url.openConnection();
} catch (IOException e) {
}
// https.setHostnameVerifier(DO_NOT_VERIFY);
http = https;
} else {
try {
http = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
}
}
Authenticator.setDefault(new MyAuthenticator());
return http;
}
trustEveryone 方法:
HttpsURLConnection
.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname,
SSLSession session) {
return true;
}
});
SSLContext context = SSLContext.getInstance("TLS"); // TLS
context.init(null, new X509TrustManager[] { new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
} }, new SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(context
.getSocketFactory());
【问题讨论】:
-
另一个没有参数的调用总是运行顺利
-
这个问题很奇怪,因为我明白了,如果我调用其他文件,包括参数调用没有给我任何问题