java 如何在 Android 中使用 HTTPS 发布

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11504467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:21:56  来源:igfitidea点击:

How to HTTPS post in Android

javaandroidhttpshttp-post

提问by Noman Arain

I have looked at the following links, but nothing seems concrete. Secure HTTP Post in AndroidThis one does not work anymore, I have tested it and there are comments from other people saying it does not work.

我查看了以下链接,但似乎没有任何具体内容。 Android 中的 Secure HTTP Post这个已经不能用了,我已经测试过了,还有其他人的评论说它不能用。

I also checked this out: DefaultHttpClient, Certificates, Https and posting problem!This seems it could work but the blogger just leaves you hanging. More step by step instructions would be helpful. I managed to get my certificate by I have not been able to follow through his second step.

我还检查了这个:DefaultHttpClient、证书、Https 和发布问题!这似乎可以工作,但博主只是让你悬而未决。更多的分步说明会有所帮助。由于我无法完成他的第二步,我设法获得了证书。

http://www.makeurownrules.com/secure-rest-web-service-mobile-application-android.htmlThis one seem good, but again, I loose the author at the last step: "Back to our original rest client code." He too is all over the place, I have no clue which libraries he is using. He is not explaining his code and with the

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

RestTemplate restTemplate = new RestTemplate();

it's another cliffhanger. Because that class has not been provided. So, if someone could explain how to do HTTPS post request in detail that would be great. I do need to accept the self signed certificate.

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

回答by Varundroid

I hope it would help. This is the code i used and worked perfectly fine.

我希望它会有所帮助。这是我使用的代码并且工作得很好。

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

Then create an HttpClient like this: -

然后像这样创建一个 HttpClient: -

HttpClient httpClient = createHttpClient();

and use it with HttpPost.

并将其与 HttpPost 一起使用。

Cheers!!

干杯!!

EDIT

编辑

And i did not used RestTemplate in my code. I made a simple post request. If you need more help just let me know. It seems like i recently have done something similar to what you are looking for.

而且我没有在我的代码中使用 RestTemplate。我做了一个简单的post请求。如果您需要更多帮助,请告诉我。好像我最近做了一些类似于你正在寻找的东西。

回答by Kumar Vivek Mitra

This is the method i used for HTTPS Post and Here i used Custom Certificate, So change the HttpClient assignment with yours own...

这是我用于 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();
    }