从 Android 连接到 HTTPS - REST 和 SOAP Web 服务
我在从 Android 调用 https 时遇到问题。任何人都可以帮忙完成连接到 https 服务器的步骤吗?
我尝试使用 https 连接到 google,它工作正常,但是当我尝试连接到本地服务器时,我遇到了问题。
- 想要通过 https 连接 RESTful Web 服务
想要通过 https 连接使用 JAX-WS 开发的基于 SOAP 的 Web 服务。
使用 RESTFul 连接的代码
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER; DefaultHttpClient 客户端 = new DefaultHttpClient(); SchemeRegistry注册表=newSchemeRegistry(); SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory(); socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier); 注册表.注册(新方案(“https”,socketFactory,443)); SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(),registry); DefaultHttpClient httpClient = new DefaultHttpClient(mgr, client.getParams()); // 设置验证器 HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier); // 发送http请求示例 //最终字符串 url = "https://encrypted.google.com/"; 最终字符串 url = "https://ipaddress:8181/RESTTest/cars"; HttpPost httpPost = new HttpPost(url); 尝试{ HttpResponse 响应 = httpClient.execute(httpPost); System.out.println(响应); }捕获(异常e){ e.printStackTrace(); }
它对谷歌工作正常,但对我的服务器不起作用,它给了
javax.net.ssl.SSLException:不受信任的服务器证书
使用 SOAP 连接的代码:
public String getString(final String methodName, Map
params) throws Exception { HttpTransportSE httpsTransportSE = new HttpTransportSE("https://ipaddress:8181/BankingWS/banking?wsdl"); 尝试 { SoapObject 请求 = new SoapObject("https://test/", 方法名); if(params != null && params.size() > 0){ 设置<字符串>键= params.keySet(); 迭代器 迭代器=keys.iterator(); while(iterator.hasNext()){ 字符串键 = iterator.next(); request.addProperty(key, params.get(key)); 键=空; } } SoapSerializationEnvelope 信封 = new SoapSerializationEnvelope( SoapEnvelope.VER11); 信封.setOutputSoapObject(请求); TrustManagerManipulator.allowAllSSL(); httpsTransportSE.call(方法名, 信封); SoapPrimitive sp = (SoapPrimitive) 信封.getResponse(); 返回 sp.toString(); } catch (异常异常) { System.out.println(异常.toString()); 抛出异常; } } 在上面的代码中使用来自以下链接的 TrustManagerManipulator: http://abhinavasblog.blogspot.com/2011/07 /allow-untrusted-certificate-for-https.html
这也不起作用,当我检查响应代码时,它给出
SoapFault - faultcode: 'S:Client' faultstring: 'Cannot find dispatch method for {test.WSEndPointPort}authenticate' faultactor: 'null' detail: null
请帮助解决这个问题,因为我没有能够通过任何方式从 Android 调用 https :(
非常感谢您的时间和努力。
谢谢, 伊尚
I am facing a problem to call https from Android. Can any one please help with steps to connect to the https server.
I tried to connect to google with https and its working fine but when I try to connect to my local server, I am facing problems.
- want to connect a RESTful web service with https
want to connect a SOAP based web service developed using JAX-WS with https.
Code to connect with RESTFul
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://encrypted.google.com/"; final String url = "https://ipaddress:8181/RESTTest/cars"; HttpPost httpPost = new HttpPost(url); try{ HttpResponse response = httpClient.execute(httpPost); System.out.println(response); }catch(Exception e){ e.printStackTrace(); }
its working fine for google but not working for my server and it's giving
javax.net.ssl.SSLException: Not trusted server certificate
Code for connect with SOAP:
public String getString(final String methodName, Map<String, Object> params) throws Exception { HttpTransportSE httpsTransportSE = new HttpTransportSE("https://ipaddress:8181/BankingWS/banking?wsdl"); try { SoapObject request = new SoapObject("https://test/", methodName); if(params != null && params.size() > 0){ Set<String> keys = params.keySet(); Iterator<String> iterator = keys.iterator(); while(iterator.hasNext()){ String key = iterator.next(); request.addProperty(key, params.get(key)); key = null; } } SoapSerializationEnvelope envelope = new SoapSerializationEnvelope( SoapEnvelope.VER11); envelope.setOutputSoapObject(request); TrustManagerManipulator.allowAllSSL(); httpsTransportSE.call(methodName, envelope); SoapPrimitive sp = (SoapPrimitive) envelope.getResponse(); return sp.toString(); } catch (Exception exception) { System.out.println(exception.toString()); throw exception; } }
In above code using the TrustManagerManipulator from following link:
http://abhinavasblog.blogspot.com/2011/07/allow-untrusted-certificate-for-https.html
This is also not working and when I check the response code it's giving
SoapFault - faultcode: 'S:Client' faultstring: 'Cannot find dispatch method for {test.WSEndPointPort}authenticate' faultactor: 'null' detail: null
Please help to fix this problem, as I am not able to call https from Android by any way :(
Thank you very much for your time and efforts.
Thank you,
Ishan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要创建一个绕过所有安全检查的 X509TrustManager。您可以在类似的问题中找到示例:
从 Java 建立到 HTTPS 服务器的连接并忽略安全证书的有效性
或
如何忽略 Apache 中的 SSL 证书错误HttpClient 4.0
You need to create a X509TrustManager which bypass all the security check. You can find an example in this similar questions:
Make a connection to a HTTPS server from Java and ignore the validity of the security certificate
or
How to ignore SSL certificate errors in Apache HttpClient 4.0