All Projects → icecooly → FastHttpClient

icecooly / FastHttpClient

Licence: other
封装OkHttp3,对外提供了POST请求、GET请求、上传文件、下载文件、https请求、cookie管理等功能

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to FastHttpClient

Retrofiturlmanager
🔮 Let Retrofit support multiple baseUrl and can be change the baseUrl at runtime (以最简洁的 Api 让 Retrofit 同时支持多个 BaseUrl 以及动态改变 BaseUrl).
Stars: ✭ 1,961 (+3168.33%)
Mutual labels:  okhttp, okhttp3
simple-http
抽取一个简单 HTTP 的通用接口,底层实现根据具体引入依赖指定。
Stars: ✭ 38 (-36.67%)
Mutual labels:  httpclient, okhttp3
Easyhttp
Android 网络请求框架,简单易用,so easy
Stars: ✭ 423 (+605%)
Mutual labels:  httpclient, okhttp
Okhttps
如艺术一般优雅,像 1、2、3 一样简单,前后端通用,轻量却强大的 HTTP 客户端(同时支持 WebSocket 与 Stomp 协议)
Stars: ✭ 92 (+53.33%)
Mutual labels:  okhttp, okhttp3
metrics-okhttp
An OkHttp HTTP client wrapper providing Metrics instrumentation of connection pools, request durations and rates, and other useful information.
Stars: ✭ 18 (-70%)
Mutual labels:  okhttp, okhttp3
Chucker
🔎 An HTTP inspector for Android & OkHTTP (like Charles but on device)
Stars: ✭ 2,169 (+3515%)
Mutual labels:  okhttp, okhttp3
Fast Android Networking
🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀
Stars: ✭ 5,346 (+8810%)
Mutual labels:  fast, okhttp
Livedata Call Adapter
A simple LiveData call adapter for retrofit
Stars: ✭ 119 (+98.33%)
Mutual labels:  okhttp, okhttp3
iMoney
iMoney 金融项目
Stars: ✭ 55 (-8.33%)
Mutual labels:  okhttp, okhttp3
WanAndroid
wanandroid的Kotlin版,采用Android X
Stars: ✭ 20 (-66.67%)
Mutual labels:  okhttp, okhttp3
Kotlin Networking
Kotlin Networking - An elegant networking library written in Kotlin
Stars: ✭ 88 (+46.67%)
Mutual labels:  okhttp, okhttp3
BaseDevelop
an android project for now fashion open source framework
Stars: ✭ 24 (-60%)
Mutual labels:  fast, okhttp
Sttp
The Scala HTTP client you always wanted!
Stars: ✭ 1,078 (+1696.67%)
Mutual labels:  httpclient, okhttp
finch
🖥 Debug menu library for Android apps with supports network activity logging and many other useful features.
Stars: ✭ 42 (-30%)
Mutual labels:  okhttp, okhttp3
windigo-android
Windigo is easy to use type-safe rest/http client for android
Stars: ✭ 23 (-61.67%)
Mutual labels:  httpclient, okhttp
RxHttp
基于RxJava2+Retrofit+OkHttp4.x封装的网络请求类库,亮点多多,完美兼容MVVM(ViewModel,LiveData),天生支持网络请求和生命周期绑定,天生支持多BaseUrl,支持文件上传下载进度监听,支持断点下载,支持Glide和网络请求公用一个OkHttpClient⭐⭐⭐
Stars: ✭ 25 (-58.33%)
Mutual labels:  okhttp, okhttp3
greptile
Fast grep implementation in python, with recursive search and replace
Stars: ✭ 17 (-71.67%)
Mutual labels:  fast
hash-wasm
Lightning fast hash functions using hand-tuned WebAssembly binaries
Stars: ✭ 382 (+536.67%)
Mutual labels:  fast
sapper-httpclient
An isomorphic http client for Sapper
Stars: ✭ 48 (-20%)
Mutual labels:  httpclient
ehhttp
OkHttp calls as RxJava types
Stars: ✭ 19 (-68.33%)
Mutual labels:  okhttp

FastHttpClient

封装OkHttp3

  • 支持多线程异步请求
  • 支持Http/Https协议
  • 支持同步/异步请求
  • 支持异步延迟执行
  • 支持Cookie持久化
  • 支持JSON、表单提交
  • 支持文件和图片上传/批量上传,支持同步/异步上传,支持进度提示
  • 支持文件流上传

Download

Download Jar or grab via Maven:

<dependency>
  <groupId>com.github.icecooly</groupId>
  <artifactId>FastHttpClient</artifactId>
  <version>1.7</version>
</dependency>

or Gradle:

compile 'com.github.icecooly:FastHttpClient:1.7'

简单的例子

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.jpg";
String imageUrl="http://e.hiphotos.baidu.com/image/pic/item/faedab64034f78f0b31a05a671310a55b3191c55.jpg";
FastHttpClient.newBuilder().addNetworkInterceptor(new DownloadFileInterceptor(){
			@Override
			public void updateProgress(long downloadLenth, long totalLength, boolean isFinish) {
				logger.info("updateProgress downloadLenth:"+downloadLenth+
						",totalLength:"+totalLength+",isFinish:"+isFinish);
			}
		}).
		build().
		get().
		url(imageUrl).
		build().
		executeAsync(new DownloadFileCallback(savePath) {//save file to /tmp/tmp.jpg
				@Override
				public void onFailure(Call call, Exception e, int id) {
					logger.error(e.getMessage(),e);
				}
				@Override
				public void onSuccess(Call call, File file, int id) {
					logger.info("filePath:"+file.getAbsolutePath());
				}
				@Override
				public void onSuccess(Call call, InputStream fileStream, int id) {
					logger.info("onSuccessWithInputStream");
				}
});

5.同步下载文件

public void testSyncDownloadFile() throws Exception{
	String savePath="tmp.jpg";
	String imageUrl="http://e.hiphotos.baidu.com/image/pic/item/faedab64034f78f0b31a05a671310a55b3191c55.jpg";
	InputStream is=FastHttpClient.get().url(imageUrl).build().execute().byteStream();
	FileUtil.saveContent(is, new File(savePath));
}

6.上传文件

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());

7.上传文件(通过文件流)

InputStream is=new FileInputStream("/tmp/logo.jpg");
Response response = FastHttpClient.newBuilder().
		connectTimeout(10, TimeUnit.SECONDS).
		build().
		post().
		url("上传地址").
		addFile("file", "logo.jpg",is).
		build().
		execute();
logger.info(response.body().string());

8.设置网络代理

Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1088));
Authenticator.setDefault(new Authenticator(){//如果没有设置账号密码,则可以注释掉这块
	         private PasswordAuthentication authentication = 
	         		new PasswordAuthentication("username","password".toCharArray());
	         @Override
	         protected PasswordAuthentication getPasswordAuthentication(){
	             return authentication;
	         }
	     });
Response response = FastHttpClient.
		newBuilder().
		proxy(proxy).
		build().
		get().
		url("http://ip111.cn/").
		build().
		execute();
logger.info(response.string());

9.设置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());

9.设置https证书

SSLContext sslContext=getxxx();
Response response=FastHttpClient.
			get().
			sslContext(sslContext).
			url(url).
			build().
			execute();
System.out.println(response.string());

10.自动携带Cookie进行请求

private class LocalCookieJar implements CookieJar{
	    List<Cookie> cookies;
	    @Override
	    public List<Cookie> loadForRequest(HttpUrl arg0) {
	         if (cookies != null) {
	                return cookies;
	         }
	         return new ArrayList<Cookie>();
	    }
	    @Override
	    public void saveFromResponse(HttpUrl arg0, List<Cookie> cookies) {
	        this.cookies = cookies;
	    }
}
LocalCookieJar cookie=new LocalCookieJar();
HttpClient client=FastHttpClient.newBuilder()
        .followRedirects(false) //禁制OkHttp的重定向操作,我们自己处理重定向
        .followSslRedirects(false)
        .cookieJar(cookie)   //为OkHttp设置自动携带Cookie的功能
        .build();
String url="https://www.baidu.com/";
client.get().addHeader("Referer","https://www.baidu.com/").
	url(url).
	build().
	execute();
System.out.println(cookie.cookies);

11.设置Content-Type为application/json

String url="https://wx.qq.com";
Response response=FastHttpClient.
		post().
		addHeader("Content-Type","application/json").
		body("{\"username\":\"test\",\"password\":\"111111\"}").
		url(url).
		build().
		execute();

12.取消请求

RequestCall call=FastHttpClient.get().
				url("https://www.baidu.com").
				build();
Response response=call.execute();
call.cancel();
System.out.println(response.string());

13.取消所有请求

FastHttpClient.cancelAll();

14.按照TAG取消请求

FastHttpClient.cancel(tag);
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].