2018-09-28 Ok2Curl, 将OkHttp请求转换为curl日志

2018-09-30 10:17:51 浏览数 (1)

源代码名称:Ok2Curl* 源代码网址:http://www.github.com/mrmike/Ok2Curl* Ok2Curl源代码文档 Ok2Curl源代码下载

Ok2Curl Sample

Repository

Gradle

代码语言:javascript复制
repositories {
 jcenter()
}
dependencies {
 compile 'com.github.mrmike:Ok2Curl:0.4.5'}
代码语言:javascript复制
// https://mvnrepository.com/artifact/com.github.mrmike/ok2curl
compile group: 'com.github.mrmike', name: 'ok2curl', version: '0.4.5'

Maven

代码语言:javascript复制
   <repositories>
        <repository>
            <id>milestone</id>
            <name>spring milestone</name>
            <url>http://repo.spring.io/libs-milestone/</url>
        </repository>
    </repositories>
代码语言:javascript复制
<!-- https://mvnrepository.com/artifact/com.github.mrmike/ok2curl -->
<dependency>
    <groupId>com.github.mrmike</groupId>
    <artifactId>ok2curl</artifactId>
    <version>0.4.5</version>
</dependency>

如果你没有用Maven 或者gradle 来管理依赖请把下面4个文件放到你的lib目录里面

image.png

代码语言:javascript复制
    public static void main(String[] args) {
        OkHttpClient okHttp = new OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)
                .addNetworkInterceptor(new CurlInterceptor(new Loggable() {
                    @Override
                    public void log(String message) {
                        log.error(message);
                    }
                })).build();


        Request request = new Request.Builder().url("http://www.baidu.com").build();

        try {
            Response response = okHttp.newCall(request).execute();

            log.debug(response.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
代码语言:javascript复制
curl -X GET -H "Host:www.baidu.com" -H "Connection:Keep-Alive" -H "Accept-Encoding:gzip" -H "User-Agent:okhttp/3.11.0" "http://www.baidu.com/"

直接在Terminal中输入会乱码,加个参数就好了

代码语言:javascript复制
curl --compressed -X GET -H "Host:www.baidu.com" -H "Connection:Keep-Alive" -H "Accept-Encoding:gzip" -H "User-Agent:okhttp/3.8.1" "http://www.baidu.com/"

当然去掉-H "Accept-Encoding:gzip" 不通过gzip压缩就不会乱码了

代码语言:javascript复制
curl -X GET -H "Host:www.baidu.com" -H "Connection:Keep-Alive"  -H "User-Agent:okhttp/3.11.0" "http://www.baidu.com/"

0 人点赞