[英]Connect to HTTPS from Android - REST and SOAP web service
我在從Android調用https時遇到問題。 可以請任何人幫助您連接到https服務器的步驟。
我嘗試使用https連接到google,並且工作正常,但是當我嘗試連接到本地服務器時,我遇到了問題。
想要將使用JAX-WS開發的基於SOAP的Web服務與https連接起來。
連接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(); }
它的工作正常的谷歌,但不適用於我的服務器,它給
javax.net.ssl.SSLException:不可信的服務器證書
用於連接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; } }
在上面的代碼中,通過以下鏈接使用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 :(
非常感謝您的時間和精力。
謝謝你Ishan
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.