代码拉取完成,页面将自动刷新
同步操作将从 icecooly/FastHttpClient 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
封装OkHttp3(jdk8或以上)
Download Jar or grab via Maven:
<dependency>
<groupId>com.github.icecooly</groupId>
<artifactId>FastHttpClient</artifactId>
<version>1.3</version>
</dependency>
or Gradle:
compile 'com.github.icecooly:FastHttpClient:1.3'
1.同步Get请求(访问百度首页,自动处理https单向认证)
String url="https://www.baidu.com";
String resp=FastHttpClient.get().url(url).build().execute().string();
2.异步Get请求(访问百度首页)
FastHttpClient.get().url("https://www.baidu.com").build().
executeAsync(new StringCallback() {
@Override
public void onFailure(Call call, Exception e, int id) {
logger.error(e.getMessage(),e);
}
@Override
public void onSuccess(Call call, String response, int id) {
logger.info("response:{}",response);
}
});
3.百度搜索关键字'微信机器人'
String html = FastHttpClient.get().
url("http://www.baidu.com/s").
addParams("wd", "微信机器人").
addParams("tn", "baidu").
build().
execute().
string();
4.异步下载一张百度图片,有下载进度,保存为/tmp/tmp.jpg
String savePath="/tmp/tmp.jpg";
FastHttpClient.get().
url("http://e.hiphotos.baidu.com/image/pic/item/faedab64034f78f0b31a05a671310a55b3191c55.jpg").
build().addNetworkInterceptor(new DownloadFileInterceptor(){
@Override
public void updateProgress(long downloadLenth, long totalLength, boolean isFinish) {
System.out.println("updateProgress downloadLenth:"+downloadLenth+
",totalLength:"+totalLength+",isFinish:"+isFinish);
}
}).
executeAsync(new DownloadFileCallback(savePath) {
@Override
public void onFailure(Call call, Exception e, int id) {
e.printStackTrace();
}
@Override
public void onSuccess(Call call, File file, int id) {
super.onSuccess(call, file, id);
System.out.println("filePath:"+file.getAbsolutePath());
}
@Override
public void onSuccess(Call call, InputStream fileStream, int id) {
System.out.println("onSuccessWithInputStream");
}
});
5.上传文件
byte[] imageContent=FileUtil.getBytes("/tmp/test.png");
response = FastHttpClient.post().
url(url).
addFile("file", "b.jpg", imageContent).
build().
connTimeOut(10000).
execute();
System.out.println(response.body().string());
6.设置网络代理
Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1088));
Response response = FastHttpClient.
newBuilder().
addNetworkInterceptor(logging).
proxy(proxy).
build().
get().
url("http://www.baidu.com").
build().
execute();
logger.info(response.string());
7.设置Http头部信息
String url="https://www.baidu.com";
Response response=FastHttpClient.
get().
addHeader("Referer","http://news.baidu.com/").
addHeader("cookie", "uin=test;skey=111111;").
url(url).
build().
execute();
System.out.println(response.string());
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。