2012-07-16 25 views
8

我看了下面的鏈接,但沒有看到具體的內容。 Secure HTTP Post in Android 這一個不工作了,我測試了它,並有其他人的評論說它不起作用。HTTPS如何在Android上發帖

我也檢查了這一點:DefaultHttpClient, Certificates, Https and posting problem!這似乎可以工作,但博客只是讓你掛。更多的分步說明會有所幫助。我設法得到我的證書,因爲我無法完成他的第二步。

http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html這一個看起來不錯,但是我最後一步放棄了作者:「回到我們原來的休息客戶端代碼。」他也到處都是,我不知道他正在使用哪些圖書館。他沒有解釋他的代碼和

RestTemplate restTemplate = new RestTemplate(); 

這是另一個cliffhanger。因爲那個班沒有提供。所以,如果有人能夠詳細解釋如何完成HTTPS發佈請求,那就太棒了。我確實需要接受自簽名證書。

回答

11

我希望這會有所幫助。這是我使用的代碼,工作得很好。

private HttpClient createHttpClient() 
{ 
    HttpParams params = new BasicHttpParams(); 
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); 
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); 
    HttpProtocolParams.setUseExpectContinue(params, true); 

    SchemeRegistry schReg = new SchemeRegistry(); 
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); 
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg); 

    return new DefaultHttpClient(conMgr, params); 
} 

然後創建一個HttpClient的是這樣的: -

HttpClient httpClient = createHttpClient(); 

與HttpPost使用它。

乾杯!

編輯

,我沒有在我的代碼使用RestTemplate。我做了一個簡單的發佈請求。如果您需要更多幫助,請告訴我。看起來我最近做了類似於你要找的東西。

0

這是我用於HTTPS後,在這裏我使用自定義證書,所以改變與你HttpClient的分配自己的方法......

public String postData(String url, String xmlQuery) { 



     final String urlStr = url; 
     final String xmlStr = xmlQuery; 
     final StringBuilder sb = new StringBuilder(); 



     Thread t1 = new Thread(new Runnable() { 

      public void run() { 

       HttpClient httpclient = MySSLSocketFactory.getNewHttpClient(); 

       HttpPost httppost = new HttpPost(urlStr); 


       try { 

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
          1); 
        nameValuePairs.add(new BasicNameValuePair("xml", xmlStr)); 

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        HttpResponse response = httpclient.execute(httppost); 

        Log.d("Vivek", response.toString()); 

        HttpEntity entity = response.getEntity(); 
        InputStream i = entity.getContent(); 

        Log.d("Vivek", i.toString()); 
        InputStreamReader isr = new InputStreamReader(i); 

        BufferedReader br = new BufferedReader(isr); 

        String s = null; 


        while ((s = br.readLine()) != null) { 

         Log.d("YumZing", s); 
         sb.append(s); 
        } 


        Log.d("Check Now",sb+""); 




       } catch (ClientProtocolException e) { 

        e.printStackTrace(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } /* 
       * catch (ParserConfigurationException e) { // TODO 
       * Auto-generated catch block e.printStackTrace(); } catch 
       * (SAXException e) { // TODO Auto-generated catch block 
       * e.printStackTrace(); } 
       */ 
      } 

     }); 

     t1.start(); 
     try { 
      t1.join(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     System.out.println("Getting from Post Data Method "+sb.toString()); 

     return sb.toString(); 
    } 
+0

「這是我用於HTTP Post和Here的方法,我使用了自定義證書,因此請使用您自己的方式更改HttpPost作業...」我需要使用HTTPS發佈而不是http發佈。巨大差距。 – 2012-07-16 12:43:39

+0

抱歉,我的錯誤HTTPS,它使用HTTPS工作...嘗試它.. – 2012-07-16 12:45:34

+0

試試看,它會工作..... – 2012-07-16 12:46:13