//www.greatytc.com/p/1873287eed87 OKHTTP的基本用法
源代码名称:Ok2Curl*
源代码网址:http://www.github.com/mrmike/Ok2Curl*
Ok2Curl源代码文档
Ok2Curl源代码下载
Repository
Gradle
repositories {
jcenter()
}
dependencies {
compile 'com.github.mrmike:Ok2Curl:0.4.5'}
// https://mvnrepository.com/artifact/com.github.mrmike/ok2curl
compile group: 'com.github.mrmike', name: 'ok2curl', version: '0.4.5'
Maven
<repositories>
<repository>
<id>milestone</id>
<name>spring milestone</name>
<url>http://repo.spring.io/libs-milestone/</url>
</repository>
</repositories>
<!-- 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目录里面
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();
}
}
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中输入会乱码,加个参数就好了
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压缩就不会乱码了
curl -X GET -H "Host:www.baidu.com" -H "Connection:Keep-Alive" -H "User-Agent:okhttp/3.11.0" "http://www.baidu.com/"