2012-07-16 13 views
8

He mirado en los siguientes enlaces, pero nada parece hormigón. Secure HTTP Post in Android Éste ya no funciona, he probado y hay comentarios de otras personas que dicen que no funciona.Cómo HTTPS su mensaje en Android

También comprobé esto: DefaultHttpClient, Certificates, Https and posting problem! Esto parece que podría funcionar, pero el blogger simplemente te deja colgando. Más instrucciones paso a paso serían útiles. Logré obtener mi certificado porque no pude seguir su segundo paso.

http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html Éste parece bueno, pero de nuevo, pierdo al autor en el último paso: "Volver a nuestro código original de cliente de descanso". Él también está por todas partes, no tengo idea de qué bibliotecas está usando. Él no está explicando su código y con el

RestTemplate restTemplate = new RestTemplate(); 

es otra situación tensa. Porque esa clase no ha sido proporcionada. Por lo tanto, si alguien pudiera explicar cómo hacer una solicitud posterior a HTTPS en detalle, sería genial. Necesito aceptar el certificado autofirmado.

Respuesta

11

espero que ayudaría. Este es el código que usé y funcionó perfectamente bien.

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); 
} 

A continuación, cree una HttpClient así: -

HttpClient httpClient = createHttpClient(); 

y utilizarlo con HttpPost.

¡Salud!

EDITAR

y yo no utilicé RestTemplate en mi código. Hice una simple solicitud de publicación. Si necesita más ayuda solo hágamelo saber. Parece que recientemente hice algo similar a lo que estás buscando.

0

Este es el método que utiliza para HTTPS Post y Aquí he utilizado certificado personalizado, así que cambie la asignación HttpClient con los suyos propios ...

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

"Este es el método que utiliza para HTTP Post y Aquí he utilizado certificado personalizado, así que cambie la asignación HttpPost con los suyos propios ..." Tengo que trabajar con HTTPS no salen post http. Gran diferencia. –

+0

Lo siento, mi error HTTPS, FUNCIONA CON HTTPS ... PRUÉBELO ... –

+0

Pruébalo, funcionará ..... –

Cuestiones relacionadas