2011-04-06 68 views
0

我終於把我的應用程序「看似」發佈到https。但是,每次我發佈並得到結果,結果都是-200。這個結果是服務器告訴我需要進行身份驗證的響應。如果我正確認證,結果將是正面的。而且我正在發帖進行身份驗證......所以回覆只是告訴我它失敗了。我曾與服務器管理員談過,他說我可能在某個地方有實際的空間。https發帖,未收到期望值

我的問題是,我如何檢查正在發佈的完整消息?基本上,我如何檢查以確保我在網絡瀏覽器中輸入的URL與應用程序中發佈的內容相同?現在,我已經打印了一些內容,但是我怎麼知道所有這些信息都是正確的。從「https」開始直到消息的最後。

在此先感謝!任何幫助表示讚賞,如果您發現我目前有任何問題,請告訴我!謝謝!

的URL看起來應該是這樣發帖時:我從來沒有聽說過

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(); 
    } 
} 
+0

剝離掉所有避免SSL名稱驗證的代碼將有助於提高您的問題的可讀性。 – 2011-04-06 21:09:33

+0

我在那裏留下了那些東西,以便人們可以檢查可能影響問題的代碼的完整部分,因爲我不確定它是否可以正常工作。在發生此問題之前,我無法發佈到https。 – prolink007 2011-04-06 21:52:10

回答

0

我想出了我的問題是什麼!我之前實際上發佈到http之前,我將該帖子附加到URL ...哎呀。

下面列出了需要示例的帖子的固定代碼。

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

-200表示需要進行身份驗證。這是您的服務器發送的自定義響應代碼嗎?一般來說,當你需要認證時,你會得到一個401或類似的東西。

這裏是standard HTTP status codes

此外,當您發佈的參數,它們不會添加到URL中,以便您的網址不會像你放什麼在這個問題上的結束。

這將僅僅是https://ipaddress/health_monitoring/admin.php

+0

這是一個自定義響應。我如何檢查沒有額外的字符放入帖子?我將帖子打印到屏幕上,並且我沒有看到任何額外的字符,但管理員說我可能在某處放置了額外的空間。 – prolink007 2011-04-06 21:51:04

0

好一件事我只注意到的是:

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

要放置常量值"uName""pWord"到地圖,而不是變量。它應該是:

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

您可能還需要調用存在params .trim()第一,以確保沒有多餘的空格。

+0

這些是任意的,只是爲了保護實際的用戶名和密碼... – prolink007 2011-04-06 22:01:42