Android中的HttpURLConnection电线记录 [英] HttpURLConnection wire logging in Android

查看:109
本文介绍了Android中的HttpURLConnection电线记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的logcat中获取请求/响应的所有标头。似乎HttpURLConnection与org.apache.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

似乎这已从HttpURLConnection的Android实现中删除。
有没有简单的方法来嗅探logcat上的请求/响应?

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

谢谢

推荐答案

我不确定Android上的标准HttpURLConnection是否可行。因此使用OkHttp库很容易实现:

I am not sure it is possible with standard HttpURLConnection on Android. Therefore it is easy to achieve using OkHttp library:

HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);

OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
httpClientBuilder.addInterceptor(loggingInterceptor);

OkHttpClient client = httpClientBuilder.build();
Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url("http://android.com");

client.newCall(requestBuilder.build()).execute();

这篇关于Android中的HttpURLConnection电线记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆