How to catch an http GET replay using Httpurlconnection java.net?

Asked

Viewed 2,561 times

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 ex:

{
  "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 got the http fault code... I haven’t touched java in years....

1 answer

1


If I understand correctly you’re wanting to see the headers of your request response.

For this use methods HttpURLConnection.getHeader...(), as an example getHeaderFields(), which returns the key map and header values, or getHeaderFieldKey(String name), that returns the key value 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 + ", ");
    }
}
  • Thank you for the reply Piovezan! He returned me the following. null = HTTP/1.1 401 Unauthorized, WWW-Authenticate = EG1-HMAC-SHA256 Realm="Akab-xxxxxxxxxxxxxxxxxxxx.luna.akamaiapis.net", Date = Thu, 14 Jul 2016 20:11:11 GMT, Content-Length = 413, Content-Type = application/problem+json, Connection = close, I need to authenticate this way https://developer.akamai.com/introduction/Client_Auth.html

  • Oh yeah, I just thought I’d find out what comes in the answer. Then I’ll take it easy. 401 Unauthorized means authentication failed, maybe it’s a problem in the way your authentication header is being mounted.

  • I have no leads. The Authorization header of the request seems ok, my only suspicion is that the timestamp needs to be very precise by requiring the authentication API. Although the request requires UTC and the response brings GMT, try comparing the timestamp of the two to see if it is within a sufficiently small range.

  • So...before it was the timestamp, it has a delay of 30 seconds, but before when it was timestamp it was 400 not 401, I noticed the is but I’m still far from solving, Akamai has an encryption algorithm, I need to apply the encryption on my key before passing

  • That’s probably what it is. If you encounter problems applying the encryption open a new question, because then it will be a very different subject from the one made here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.