HttpURLConnection conn.getRequestProperty return null

17,125

Solution 1

Not quite sure what you really want to do. But to see what is posted to the server you would have to post it to your own and read the data you receive there.

If you want to see all the REQUEST headers you could:

for (String header : conn.getRequestProperties().keySet()) {
   if (header != null) {
     for (String value : conn.getRequestProperties().get(header)) {
        System.out.println(header + ":" + value);
      }
   }
}

Or after connecting you can print out the RESPONSE headers:

for (String header : conn.getHeaderFields().keySet()) {
   if (header != null) {
     for (String value : conn.getHeaderFields().get(header)) {
        System.out.println(header + ":" + value);
      }
   }
}

Solution 2

I would suggest using Apache HttpClient

final HttpClient client = new HttpClient();
final PostMethod method = new PostMethod(uri);
method.addRequestHeader("X-Rim-Push-Title", "-message");
client.executeMethod(method);
String responseBody = method.getResponseBodyAsString();
Header[] headers = method.getResponseHeaders();

HttpClient is a much more powerful way of dealing with HTTP than HttpURLConnection.

Share:
17,125
dzgeek
Author by

dzgeek

experimented java developer they say that copying is innoving ...

Updated on July 26, 2022

Comments

  • dzgeek
    dzgeek almost 2 years

    I'm trying to push some data to an URL (MDS_CS) for a BES

    when i set some Request Headers in my code, and submit the request, the submited request's header is set to null.

    here is my code :

            HttpURLConnection conn =(HttpURLConnection)url.openConnection();
            conn.setDoInput(true);//For receiving the confirmation
            conn.setDoOutput(true);//For sending the data
            conn.setRequestMethod("POST");//Post the data to the proxy
            conn.setRequestProperty("X-Rim-Push-ID", pushId);
            conn.setRequestProperty("Content-Type", "text/html");
            conn.setRequestProperty("X-Rim-Push-Title", "-message");
            conn.setRequestProperty("X-Rim-Push-Type", "browser-message");                 
            conn.setRequestProperty("X-Rim-Push-Dest-Port", "7874");            
            //Write the data
            OutputStream out = conn.getOutputStream();
            out.write(data.getBytes());
            out.close();
    
            System.out.println(conn.getHeaderField("X-Rim-Push-ID"));
    

    the last line return null when i try to retrieve the X-Rim-Push-Title it is NULL only X-Rim-Push-ID which is correctly retrieved,

    please help me