有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Android中发布HTTPS

我已经看了以下链接,但似乎没有什么具体内容。 Secure HTTP Post in Android 这个已经不起作用了,我已经测试过了,还有其他人评论说它不起作用

我还检查了这个:DefaultHttpClient, Certificates, Https and posting problem!这似乎是可行的,但是博主让你挂了。更多的分步说明会有所帮助。我设法拿到了我的证书,但我没能完成他的第二步

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

RestTemplate restTemplate = new RestTemplate();

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


共 (2) 个答案

  1. # 1 楼答案

    这是我在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();
        }
    
  2. # 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。我提出了一个简单的发帖请求。如果你需要更多帮助,请告诉我。看起来我最近做了一些和你想做的相似的事情