Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpURLConnection wire logging in Android

Tags:

java

android

http

I'm trying to get all the headers for request/response in my logcat. Seems there is no easy way with HttpURLConnection as it is with org.apache.http.

According to this blog you can do:

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

Seems this was removed from Android implementation for HttpURLConnection. Is there any easy way to sniff requests/responses on logcat?

like image 418
Daniel Bogdan Avatar asked Nov 30 '25 03:11

Daniel Bogdan


1 Answers

This is something you can easily do yourself:

private static void logConnection(HttpURLConnection httpConnection) throws IOException {
    int status = httpConnection.getResponseCode();
    Log.d("logConnection", "status: " + status);
    for (Map.Entry<String, List<String>> header : httpConnection.getHeaderFields().entrySet()) {
        Log.d("logConnection", header.getKey() + "=" + header.getValue());
    }
}
like image 138
Rhand Avatar answered Dec 02 '25 15:12

Rhand



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!