简体   繁体   中英

See detail log in HttpURLConnection

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.

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);
}

//2. simple header informations:

conn.getHeaderFields().toString(); //response headers
conn.getResponseCode();//response http code
conn.getRequestProperties().toString();//request headers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM