Debug a HttpURLConnection problem

Take this example.

import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;

public class HttpConnect {
    public static void main(String[] args) throws Exception {
      URL url = new URL("http://www.rgagnon.com/howto.html");
      URLConnection con = url.openConnection();
      Reader reader = new InputStreamReader(con.getInputStream());
      while (true) {
        int ch = reader.read();
        if (ch==-1) {
          break;
        }
        System.out.print((char)ch);
      }
    }
}

The above snippet reads the HTML page and dumps it to the console.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD>
<!-- developpe par  Real Gagnon, Quebec Canada -->
<LINK REL="SHORTCUT ICON" HREF="http://www.maixuanviet.com/favicon.ico">
<META NAME="description"
      Content="Real's JAVA JAVASCRIPT WSH and PowerBuilder How-to pages with useful code snippets">
<META NAME="keywords"
      Content="java,javascript,wsh,vbscript,how-to,powerbuilder">
<LINK title="mystyle" href="howto.css" type="text/css" rel="stylesheet">
...

To trace what is going on at the HTTP protocol level, we can switch the HttpURLConnection/UrlConnection in debug mode.

You must enable the java-logging mechanism by setting a special property when starting the JVM :

java.exe -Djava.util.logging.config.file=logging.properties HttpConnect

and put in logging.properties file (by default in JRE_HOME\lib) the following property

sun.net.www.protocol.http.HttpURLConnection.level = ALL

Note :
If running from Eclipse, you put the -Djava.util.logging… via the Run configuration dialog -> Arguments Tab -> VM arguments textarea.

The result is :

2010-08-07 00:00:31 sun.net.www.protocol.http.HttpURLConnection writeRequests
FIN: sun.net.www.MessageHeader@16caf435 pairs: {GET /howto.html HTTP/1.1: null}{User-Agent: Java/1.6.0_20}{Host: www.maixuanviet.com}{Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2}{Connection: keep-alive}
2010-08-07 00:00:31 sun.net.www.protocol.http.HttpURLConnection getInputStream
FIN: sun.net.www.MessageHeader@5ac0728 pairs: {null: HTTP/1.1 200 OK}{Date: Sat, 07 Aug 2010 04:00:33 GMT}{Server: Apache}{Accept-Ranges: bytes}{Content-Length: 17912}{Keep-Alive: timeout=5, max=64}{Connection: Keep-Alive}{Content-Type: text/html}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD>
<!-- developpe par  Real Gagnon, Quebec Canada -->
<LINK REL="SHORTCUT ICON" HREF="http://www.maixuanviet.com/favicon.ico">
<META NAME="description"
      Content="Real's JAVA JAVASCRIPT WSH and PowerBuilder How-to pages with useful code snippets">
...

Done! Happy Coding!

Related posts:

Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
How to Break from Java Stream forEach
Java Program to Implement Hash Tables with Quadratic Probing
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
ClassNotFoundException vs NoClassDefFoundError
Retrieve User Information in Spring Security
Quick Guide to Spring Controllers
Một số ký tự đặc biệt trong Java
Java Program to Solve Set Cover Problem assuming at max 2 Elements in a Subset
Request Method Not Supported (405) in Spring
Java Perform to a 2D FFT Inplace Given a Complex 2D Array
Serialize Only Fields that meet a Custom Criteria with Jackson
Custom Thread Pools In Java 8 Parallel Streams
XML Serialization and Deserialization with Jackson
Java Program to Check if a Matrix is Invertible
Sao chép các phần tử của một mảng sang mảng khác như thế nào?
Một số nguyên tắc, định luật trong lập trình
Apache Commons Collections OrderedMap
Apache Commons Collections SetUtils
Guide to Java Instrumentation
Java Program to Implement Unrolled Linked List
Tính đa hình (Polymorphism) trong Java
New Features in Java 14
Hướng dẫn sử dụng biểu thức chính quy (Regular Expression) trong Java
Receive email using IMAP
Spring Security Authentication Provider
Java program to Implement Tree Set
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Spring Cloud AWS – RDS
Using JWT with Spring Security OAuth (legacy stack)
Spring MVC Content Negotiation
Encode/Decode to/from Base64