How do I get the response of an HTTP GET using HttpURLConnection java.net?

1

I have the following class:

package com.akamai.edgegrid.auth;

import java.io.*;
import java.net.*;

public class C {

   public static String getHTML(String urlToRead) throws Exception {
      StringBuilder result = new StringBuilder();
      URL url = new URL(urlToRead);
      HttpURLConnection conn = (HttpURLConnection) url.openConnection();
      conn.setRequestMethod("GET");
      conn.setRequestProperty("Accept-Charset", "UTF-8");
      conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
      conn.setRequestProperty("Host", "akab-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.luna.akamaiapis.net");
      conn.addRequestProperty("Authorization", "EG1-HMAC-SHA256 client_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;timestamp=20160714T19:25:00+0000;nonce=8b97e62f-e5d6-4a57-8f64-db423f24b7ee;signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));     
      String line;
      while ((line = rd.readLine()) != null) {
       result.append(line);
      }
      rd.close();
      return result.toString();
   }

   public static void main(String[] args) throws Exception
   {
     System.out.println(getHTML("https://akab-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.luna.akamaiapis.net/alerts/v1/portal-user?status=active"));
   }
}

I take back one:

Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.luna.akamaiapis.net/papi/v0/contracts
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at com.akamai.edgegrid.auth.C.getHTML(C.java:17)
at com.akamai.edgegrid.auth.C.main(C.java:28)

But I wanted to see the header back eg:

{
  "type": "https://problems./-/pep-authn/request-error",
  "title": "Bad request",
  "status": 401,
  "detail": "Error message is here",
  "instance": "https://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.luna.akamaiapis.net/papi/v0/contracts",
  "method": "GET",
  "serverIp": "23.45.226.189",
  "clientIp": "200.220.180.109",
  "requestId": "34799682",
  "requestTime": "2016-07-14T19:32:10Z"
}

I just have the failure http code ... for years I have not moved with java ... I'm getting caught.

    
asked by anonymous 14.07.2016 / 21:35

1 answer

1

If you understand correctly you are wanting to see the headers of your request's response.

For this, use HttpURLConnection.getHeader...() methods, such as getHeaderFields() , which returns the key map and values of the header, or getHeaderFieldKey(String name) , which returns the value of the key name .

To iterate through the map:

Map<String, List<String>> responseMap = connection.getHeaderFields();
for (String chave : responseMap.keySet()) {
    System.out.println(chave + " = ");

    List<String> valores = responseMap.get(chave);
    for (int i = 0; i < valores.size(); i++) {
        String valor = valores.get(i);
        System.out.println(valor + ", ");
    }
}
    
14.07.2016 / 21:54