0

我在 android 2.2 上执行一些 POST 请求时遇到一些问题,在较新的版本 4.0+ 中一切正常。我正在做的请求也是一个 https 请求。我总是收到 400 Bad 请求。这是我正在使用的代码:

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>
4

1 回答 1

0

我发现了我的问题。出于某种奇怪的原因。当用我的hearerMap 添加我的标题时,它添加了额外的空格。这更正了我的标题:

connection.setRequestProperty(headerMap.getKey().toString().replaceAll("\\s",""), headerMap.getValue().toString().replaceAll("\\s",""));
于 2013-02-22T18:52:36.707 回答