我正在开发一个Android应用程序来访问一些battle.net(https://eu.battle.net)帐户数据(对于魔兽世界),我正在使用org.apache.http.client.HttpClient这样做.
这是我正在使用的代码:
public static final String USER_AGENT = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 (.NET CLR 3.5.30729)";
public static class MyHttpClient extends DefaultHttpClient {
final Context context;
public MyHttpClient(Context context) {
super();
this.context = context;
}
@Override
protected ClientConnectionManager createClientConnectionManager() {
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
// Register for port 443 our SSLSocketFactory with our keystore
// to the ConnectionManager
registry.register(new Scheme("https", newSslSocketFactory(), 443));
return new SingleClientConnManager(getParams(), registry); …Run Code Online (Sandbox Code Playgroud) 我想将https请求发送到我自己的服务器站点https://10.2.20.20/fido/EzPay/login.php并从中获取响应,并将其保存为例如字符串.我在互联网上找到了一些示例代码,并尝试测试它们以解决我的问题,但它们没有帮助.下面我介绍一些我测试过的代码部分.
我试试这段代码,但我总是得到同样的例外"没有同行证书"为什么?
try
{
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient client = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams());
// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
// Example send http request
final String url = "https://10.2.20.20/fido/EzPay/login.php";
HttpPost httpPost = new HttpPost(url);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = …Run Code Online (Sandbox Code Playgroud)