2013-02-22 65 views
0

我有一些問題在Android 2.2上做一些POST請求,在新版本4.0+中一切工作正常。我正在做的請求也是一個https請求。我總是得到一個400壞請求。下面是我使用的代碼:在Android 2.2上POST與HttpURLConnection的問題

public String postRequest(String myurl, String requestBody) throws IOException { 
    try { 
       //Create connection 
       URL url = new URL(myurl); 
       connection = (HttpURLConnection)url.openConnection(); 
       connection.setRequestMethod("POST"); 

       for (Map.Entry<String, String> headerMap : httpHeaders.entrySet()) { 
        connection.setRequestProperty(headerMap.getKey(), headerMap.getValue()); 
        Log.d("HttpRequest", " headers Key = " + headerMap.getKey() + ", Value = " + headerMap.getValue()); 
       } 

       connection.setUseCaches (false); 
       connection.setDoOutput(true); 


       if(requestBody != null && requestBody.length() > 0) { 
        connection.setRequestProperty("Content-Length", "" + 
          Integer.toString(requestBody.getBytes().length)); 

        connection.setFixedLengthStreamingMode(requestBody.getBytes().length); 
    //   connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

    //   Log.v("RESPONSE MESSAGE", connection.getResponseMessage()); 
        //Send request 
        OutputStream out = connection.getOutputStream(); 
        out.write(requestBody.getBytes()); 
        out.flush(); 
        out.close(); 
        Log.v("RESPONSE MESSAGE", myurl + " "+ connection.getResponseMessage()); 
       } 

       int responseCode = connection.getResponseCode(); 
       Log.w("HttpRequest rponse code:", myurl+" - "+responseCode); 
       if(responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_ACCEPTED) { 
        // Get Response  
        InputStream is = connection.getInputStream(); 
        BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
        String line; 
        StringBuffer response = new StringBuffer(); 
        while((line = rd.readLine()) != null) { 
        response.append(line); 
        response.append('\r'); 
        } 
        rd.close(); 

        return response.toString(); 
       } else { 
    //   System.setProperty("http.keepAlive", "false"); 
        InputStream error = ((HttpURLConnection) connection).getErrorStream(); 
        Log.e("HTTP error", "Error, request failed code: "+ responseCode + " "+connection.getResponseMessage() +" \n " + error); 
        return null; 
       } 

      } catch (Exception e) { 

       e.printStackTrace(); 
       return null; 

      } finally { 

       if(connection != null) { 
       connection.disconnect(); 
       } 
      } 
} 

此外,我發佈一個XML主體。

<SetAchievementsCompletedByConsoleSpecificKeyRESTXML xmlns='http://tempuri.org/'><platformActionKeys xmlns:ns='http://schemas.microsoft.com/2003/10/Serialization/Arrays'><ns:string>1</ns:string></platformActionKeys></SetAchievementsCompletedByConsoleSpecificKeyRESTXML> 
+0

由於POST請求通常是「application/x-www-form-urlencoded」,您在哪裏指定此內容的MIME類型?你的服務器日誌告訴你什麼? – CommonsWare 2013-02-22 18:23:30

+0

如果我只是發佈一個XML體而不是表單值,是否需要應用程序/ x-ww-form-urlencoded?我得到一個400響應代碼壞請求 – Frank 2013-02-22 18:24:33

+0

「如果我只是發佈一個XML體,而不是形式值,是否有必要申請/ x-ww-form-urlencoded?」 - 'application/x-ww-form-urlencoded'在這種情況下是錯誤的,但是有時服務器對於任何數據('text/xml'?)都期待適當的MIME類型。你必須問問誰寫了你的服務器什麼是預期的,可能在你問他怎麼看服務器日誌以獲得比400響應代碼更多的信息的同時。 – CommonsWare 2013-02-22 18:27:46

回答

0

我發現我的問題。出於某種奇怪的原因。當我的聽筒地圖添加我的頭時,它添加了額外的空格。這糾正了我的標題:

connection.setRequestProperty(headerMap.getKey().toString().replaceAll("\\s",""), headerMap.getValue().toString().replaceAll("\\s",""));