【问题标题】:How to HTTPS post in Android如何在 Android 中使用 HTTPS 发布
【发布时间】:2012-07-15 07:26:30
【问题描述】:

我查看了以下链接,但似乎没有具体内容。 Secure HTTP Post in Android 这个不行了,我已经测试过了,还有其他人的cmet说它不行。

我也检查了这个:DefaultHttpClient, Certificates, Https and posting problem! 这似乎可行,但博主只是让你悬而未决。更多分步说明会有所帮助。我设法获得了我的证书,因为我无法完成他的第二步。

http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.html这个看起来不错,但我又在最后一步失去了作者:“回到我们原来的rest客户端代码。”他也到处都是,我不知道他在使用哪些图书馆。他没有解释他的代码,而是用

RestTemplate restTemplate = new RestTemplate();

这是另一个悬念。因为该课程尚未提供。因此,如果有人可以详细解释如何进行 HTTPS 发布请求,那就太好了。我确实需要接受自签名证书。

【问题讨论】:

    标签: java android https http-post


    【解决方案1】:

    这是我用于 HTTPS Post 的方法,这里我使用了自定义证书,所以用你自己的更改 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();
        }
    

    【讨论】:

    • “这是我用于 HTTP Post 的方法,在这里我使用了自定义证书,所以用你自己的更改 HttpPost 分配......”我需要使用 HTTPS 发布而不是 http 发布。大不同。
    • 而且没有HttpsPost,只有HttpPost...所以请尝试一下
    • 这不好,你不应该使用这样的线程,你应该在后台线程中这样做。 T1结束后如何让主线程知道?
    • 自签名证书在哪里处理?
    • 它是一个 pojo,另一个 java 类,它有一个返回 HttpClient 的静态方法。是的,我需要进行更正,您必须更改 HttpClient 而不是 HttpPost。它在代码中是正确的
    【解决方案2】:

    我希望它会有所帮助。这是我使用的代码,运行良好。

    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。我提出了一个简单的发布请求。如果您需要更多帮助,请告诉我。看来我最近做了一些类似于你正在寻找的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-28
      • 2013-10-02
      • 2015-02-27
      • 1970-01-01
      • 2012-05-11
      • 2012-01-03
      • 2017-07-18
      相关资源
      最近更新 更多