【问题标题】:How to POST to an HTTPS Site in android如何在 android 中发布到 HTTPS 站点
【发布时间】:2017-05-03 12:52:54
【问题描述】:

我是 android 编程的新手,因为我得到了一个副项目。 我有这个网页https://travelads.katakwe.co.za/androidlogin 它有一个用户名和密码字段,我想用它来登录我的 android 应用程序。
所以我有两个问题:
1) 能做到吗
2) 我该怎么做

我发现了以下问题:

所有这些我遇到的问题是他们没有解释他们实现的所有方法,我不知道如何将它应用到我自己的网页上

URL url =new URL("https://travelads.katakwe.co.za/androidlogin");

        HttpsURLConnection connection= (HttpsURLConnection) url.openConnection();
        String urlParameters="sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Username","name");
        connection.setRequestProperty("Password","pass");

        connection.setDoOutput(true);
        DataOutputStream dStream =new DataOutputStream(connection.getOutputStream());

        dStream.writeBytes(urlParameters);
        dStream.flush();
        dStream.close();

        int responseCode=connection.getResponseCode();
        ret=responseCode;

【问题讨论】:

  • 问题是它仍然没有解释所有的方法和它们是如何工作的,我希望能有更深入的了解
  • 他们实现的方法是什么意思?你熟悉 HTTP 吗?
  • 我也想知道我发布到的网页格式是否正确,或者这个问题是否有效
  • 不是通过android没有@nandsito

标签: android https


【解决方案1】:

我已经为 HTTPS 服务器的所有 POST/PUT/GET 请求编写了一个函数

一个。您必须处理自签名证书的内容。请务必包括在内。

b.您必须告诉要发送到服务器的内容类型类型。

c。取决于服务器配置。在处理 Android 请求之前,您通过 POSTMAN 或 curl 命令检查发送到服务器的任何请求以验证是否正常工作。

public String createConnection (String urlS, String methodInvoked,String patchBody, String postBody,String putBody){
        URL url ;
        BufferedReader br = null;
        String toBeReturned="";
        try {
            url = new URL(urlS);

            HostnameVerifier hostnameVerifier = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return true;
                }
            };
            TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        public X509Certificate[] getAcceptedIssuers() {
                            X509Certificate[] myTrustedAnchors = new X509Certificate[0];
                            return myTrustedAnchors;
                        }
                        @Override
                        public void checkClientTrusted(X509Certificate[] certs, String authType) {
                        }
                        @Override
                        public void checkServerTrusted(X509Certificate[] certs, String authType) {
                        }
                    }
            };

            // Create an SSLContext that uses our TrustManager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, null);


            HttpsURLConnection  connection = (HttpsURLConnection) url.openConnection();
            connection.setConnectTimeout(60000);
            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            connection.setSSLSocketFactory(sc.getSocketFactory());
            connection.setHostnameVerifier(hostnameVerifier);


            if (patchBody  != null ){
                Log.i(TAG, " createConnection with PATH with body" );
                connection.setRequestMethod("PATCH");
                connection.setRequestProperty("data",patchBody);
                connection.addRequestProperty("Content-Type", "application/json");
                DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
                dStream.writeBytes(patchBody);
                dStream.flush();
                dStream.close();
            }
            if (methodInvoked.equalsIgnoreCase("PATCH") && patchBody == null ){
                Log.i(TAG, " createConnection with PATH without body" );
                connection.setRequestMethod("PATCH");
//              connection.addRequestProperty("Content-Type", "application/json");
//              connection.setDoOutput(true);
            }
            if (postBody != null){
                Log.i(TAG, " createConnection with POST with body" );
                connection.setRequestMethod("POST");
                connection.addRequestProperty("Content-Type", "application/json");
                connection.setDoOutput(true);
                DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
                dStream.writeBytes(postBody);
                dStream.flush();
                dStream.close();
            }

            if (methodInvoked.equalsIgnoreCase("POST") && postBody == null ){
                Log.i(TAG, " createConnection with POST without body" );
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
                //connection.addRequestProperty("Content-Type", "application/json");
            }

            if (putBody != null){
                Log.i(TAG, " createConnection with PUT with body" );
                connection.setRequestMethod("PUT");
                connection.setDoOutput(true);
                connection.addRequestProperty("Content-Type", "application/json");
                DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
                dStream.writeBytes(putBody);
                dStream.flush();
                dStream.close();
            }



            responseCode = connection.getResponseCode();
            InputStream in= null;
            if(responseCode >= HttpsURLConnection.HTTP_BAD_REQUEST)
            {   

                in = connection.getErrorStream();
                br = new BufferedReader( new InputStreamReader(connection.getErrorStream()));
                StringBuilder sb = new StringBuilder();
                String line = null; 
                while ((line = br.readLine()) != null) {
                    sb.append(line+"\n");
                }
                 String toBeReturned_1 = sb.toString();
                 Log.i(TAG, " createConnetion error received " +  responseCode  + "  " + toBeReturned_1) ;

            }
            else{


                br = new BufferedReader( new InputStreamReader(connection.getInputStream()));
                StringBuilder sb = new StringBuilder();
                String line = null; 
                while ((line = br.readLine()) != null) {
                    sb.append(line+"\n");
                }
                toBeReturned = sb.toString();


            }


        } catch (MalformedURLException e) {
            error = e.getMessage();
            e.printStackTrace();
        } catch (IOException e) {
            error = e.getMessage();
            e.printStackTrace();
        } catch (KeyManagementException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{
            try {
                if (br!=null)
                    br.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
        Log.i(TAG, " createConnetion  finally returned" +  toBeReturned );
        return toBeReturned; 
    }

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • @Dariovieira 你也可以投票回答吗,你刚刚接受了。
  • 嗨,我认为它可以正常工作,因为我收到了 200 响应代码,但事实并非如此,当我尝试读取 toBeReturned 值时,它只是原始的 html 页面。我做错了什么吗?
  • 是 GET 还是 POST 请求?
  • 这是一个 POST 请求
猜你喜欢
  • 2012-07-15
  • 2012-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-06
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多