在Android 2.2上使用POST的HttpURLConnection问题

时间:2013-02-22 17:47:19

标签: java android httpurlconnection

我在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>

1 个答案:

答案 0 :(得分:0)

我发现了我的问题。出于一些奇怪的原因。当我的头文件与我的audderMap一起添加时,它正在添加额外的空格。这纠正了我的标题:

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