【问题标题】:https posting, not receiving the expected value as resulthttps发布,未收到预期值作为结果
【发布时间】:2011-07-31 04:13:21
【问题描述】:

我终于让我的申请“看似”发布到 https。但是,每次我发布并获得结果时,结果都是-200。这个结果是来自服务器的响应,告诉我我需要进行身份验证。如果我正确验证,结果将是肯定的。我正在发布以进行身份​​验证......所以响应只是告诉我它失败了。我已经和服务器管理员谈过了,他说我可能在实际帖子的某个地方有一个空间。

我的问题是,我如何查看发布的完整消息?基本上,我如何检查以确保我在网络浏览器中的 URL 中输入的内容与应用程序中发布的内容相同?现在,我让它打印了一些,但我怎么知道所有这些信息都是正确的。从“https”开始一直到消息的最后。

提前致谢!!任何帮助表示赞赏,如果您发现我目前所拥有的有任何问题,请告诉我!谢谢!

发帖时网址应如下所示:

https://ipaddress/health_monitoring/admin.php?action=authentication&username=uName&password=pWord

//my database helper class
public class SmartDBHelper {
    private static SmartDBHelper sDBHObject;

    private SmartDBHelper() {

    }

    public static synchronized SmartDBHelper getSDBHObject() {
        if(sDBHObject == null) {
            sDBHObject = new SmartDBHelper();
        }
        return sDBHObject;
    }

    public Object clone() throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }

    /* this function is to authenticate with the database
     * it returns the id_subject, if it is greater than 0
     * authentication was successful.
     */
    public static synchronized int authenticate(String uName, String pWord) {
        Map<String, String> tempMap = new LinkedHashMap<String, String>();
        tempMap.put("action", "authentication");
        tempMap.put("username", "uName");
        tempMap.put("password", "pWord");
        try {
            String tempUrl = "https://ipaddress/health_monitoring/admin.php";
            String result = post(tempUrl, tempMap);
            Log.v("smartdbhelper post result", result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    // always verify the host - dont check for certificate
    final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                    return true;
            }
    };

    /**
     * Trust every server - dont check for any certificate
     */
    private static void trustAllHosts() {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                            return new java.security.cert.X509Certificate[] {};
                    }

                    public void checkClientTrusted(X509Certificate[] chain,
                                    String authType) throws CertificateException {
                    }

                    public void checkServerTrusted(X509Certificate[] chain,
                                    String authType) throws CertificateException {
                    }
            } };

            // Install the all-trusting trust manager
            try {
                    SSLContext sc = SSLContext.getInstance("TLS");
                    sc.init(null, trustAllCerts, new java.security.SecureRandom());
                    HttpsURLConnection
                                    .setDefaultSSLSocketFactory(sc.getSocketFactory());
            } catch (Exception e) {
                    e.printStackTrace();
            }
    }

    private static String post(String urlString, Map formParameters)
    throws MalformedURLException, ProtocolException, IOException {
        DataOutputStream ostream = null;

        trustAllHosts();
        URL tempUrl;
        tempUrl = new URL(urlString);
        HttpsURLConnection https = (HttpsURLConnection) tempUrl.openConnection();
        https.setHostnameVerifier(DO_NOT_VERIFY);

        https.setRequestMethod("POST");
        https.setDoInput(true);
        https.setDoOutput(true);
        ostream = new DataOutputStream(https.getOutputStream());

        if(formParameters != null) {
            Set parameters = formParameters.keySet();
            Iterator it = parameters.iterator();
            StringBuffer buf = new StringBuffer();

            for(int i = 0, paramCount = 0; it.hasNext(); i++) {
                String parameterName = (String) it.next();
                String parameterValue = (String) formParameters.get(parameterName);

                if(parameterValue != null) {
                    parameterValue = URLEncoder.encode(parameterValue);
                    if(paramCount > 0) {
                        buf.append("&");
                    }
                    buf.append(parameterName);
                    buf.append("=");
                    buf.append(parameterValue);
                    ++paramCount;
                }
            }
            Log.v("smartdbhelper adding post parameters", buf.toString());
            Log.v("smartdbhelper adding post parameters", https.toString());
            ostream.writeBytes(buf.toString());
        }

        if( ostream != null ) {
            ostream.flush();
            ostream.close();
        }
        Object contents = https.getContent();
        InputStream is = (InputStream) contents;
        StringBuffer buf = new StringBuffer();
        int c;
        while((c = is.read()) != -1) {
            buf.append((char)c);
            Log.v("smartdbhelper bugger", buf.toString());
        }
        https.disconnect();
        return buf.toString();
    }
}

【问题讨论】:

  • 删除所有避免 SSL 名称验证的代码将有助于使您的问题更具可读性。
  • 我把这些东西留在了那里,这样人们就可以检查我的代码中可能影响问题的全部部分,因为我不确定它是否能正常工作。在此问题开始发生之前,我在发布到 https 时遇到了问题。

标签: java android http https http-post


【解决方案1】:

我从未听说过 -200 表示需要进行身份验证。那是您的服务器发送的自定义响应代码吗?一般来说,当你需要进行身份验证时,你会得到一个 401 或类似的。

这里是standard HTTP status codes

此外,当您发布参数时,它们不会添加到 URL 的末尾,因此您的 URL 不会像您在问题顶部放置的那样。

只是https://ipaddress/health_monitoring/admin.php

【讨论】:

  • 这是一个自定义响应。我如何检查帖子中没有多余的字符?我将帖子打印到屏幕上,我没有看到任何额外的字符,但管理员说我可能在某处放置了额外的空间。
【解决方案2】:

我刚刚注意到的一件事是:

tempMap.put("username", "uName");
tempMap.put("password", "pWord");

您将常量值 "uName""pWord" 放入映射而不是变量。应该是:

tempMap.put("username", uName);
tempMap.put("password", pWord);

您可能还想先在参数上调用.trim(),以确保没有多余的空格。

【讨论】:

    【解决方案3】:

    我知道我的问题是什么!在将帖子附加到 url 之前,我实际上是在发布到 http...哎呀。

    下面列出了帖子的固定代码,供需要示例的人使用。

    private static String post(String urlString, Map formParameters)
    throws MalformedURLException, ProtocolException, IOException {
        DataOutputStream ostream = null;
    
        trustAllHosts();
        URL tempUrl;
        StringBuffer buf = new StringBuffer();
        if(formParameters != null) {
            Set parameters = formParameters.keySet();
            Iterator it = parameters.iterator();
            //StringBuffer buf = new StringBuffer();
    
            for(int i = 0, paramCount = 0; it.hasNext(); i++) {
                String parameterName = (String) it.next();
                String parameterValue = (String) formParameters.get(parameterName);
    
                if(parameterValue != null) {
                    parameterValue = URLEncoder.encode(parameterValue);
                    if(paramCount > 0) {
                        buf.append("&");
                    }
                    buf.append(parameterName);
                    buf.append("=");
                    buf.append(parameterValue);
                    ++paramCount;
                }
            }
            Log.v("smartdbhelper adding post parameters", buf.toString());
    
    
        }
        urlString = urlString + "?" + buf;
        Log.v("smartdbhelper url string", urlString);
        tempUrl = new URL(urlString);
        HttpsURLConnection https = (HttpsURLConnection) tempUrl.openConnection();
        https.setHostnameVerifier(DO_NOT_VERIFY);
        Log.v("smartdbhelper adding post parameters", https.toString());
        https.setRequestMethod("POST");
        https.setDoInput(true);
        https.setDoOutput(true);
        ostream = new DataOutputStream(https.getOutputStream());
        ostream.writeBytes(buf.toString());
    
    
    if( ostream != null ) {
        ostream.flush();
            ostream.close();
        }
        Object contents = https.getContent();
        InputStream is = (InputStream) contents;
        StringBuffer buf2 = new StringBuffer();
        int c;
        while((c = is.read()) != -1) {
            buf2.append((char)c);
            Log.v("smartdbhelper bugger", buf2.toString());
        }
        https.disconnect();
        return buf2.toString();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-30
      • 2018-04-15
      • 1970-01-01
      • 2021-12-20
      • 2022-07-15
      • 2013-02-15
      相关资源
      最近更新 更多