Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See detail log in HttpURLConnection

Tags:

java

http

I write following code:

public static void main(String args[]) throws IOException {
    URL url = new URL("http://stackoverflow.com/");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    System.out.print(conn.getResponseMessage());
}
  1. I want show all debug information in console when i run this method.
  2. I want see what kind of http request have been sent from log.
like image 824
Bensson Avatar asked Oct 28 '25 10:10

Bensson


2 Answers

Since Java uses by default Java Logging, add the following code:

static {
    ConsoleHandler handler = new ConsoleHandler();
    handler.setLevel(Level.ALL);
    Logger log = LogManager.getLogManager().getLogger("");
    log.addHandler(handler);
    log.setLevel(Level.ALL);
}
like image 198
Paul Vargas Avatar answered Oct 29 '25 23:10

Paul Vargas


//2. simple header informations:

conn.getHeaderFields().toString(); //response headers
conn.getResponseCode();//response http code
conn.getRequestProperties().toString();//request headers
like image 32
scherzne.betti Avatar answered Oct 30 '25 00:10

scherzne.betti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!