11 Star 20 Fork 5

terrason / HttpFacade

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
contribute
Sync branch
Cancel
Notice: Creating folder will generate an empty file .keep, because not support in Git
Loading...
README
MIT

#HttpFacade

一款轻量级(Very Very Light)HTTP客户端工具. 大部分代码拷贝自jsoup,但去除了HTML的解析部分。

HttpFacade 能做什么?

使用简单API发起HTTP请求

  1. 简单HTTP请求
String body = HttpFacade.connect("http://.....")
  .data("arg1","a parameter")
  .data("arg2","another parameter")
  .get();//or post()

.data()方法加入的参数默认都会进行URLEncode编码,编码方式默认为UTF-8. body为返回值字符串。

  1. POST报文体
HttpFacade.connect("http://.....")
    .post("<xml><message>I am a message post to server.</message></xml>");

需要注意的是,使用.data()方法加入的参数会追加到URL的?后面而不会干扰您传入的报文体,这种直接POST的报文不会进行URLEncode。如果需要,你可以自己先进行编码:

String xml=URLEncoder.encode("<xml>...</xml>", "UTF-8")
HttpFacade.connect("http://.....")
    .post(xml);
  1. 其它自定义选项
String body = HttpFacade.connect("http://....../")
    .ignoreBlankParameters(false)//空字符串(包括null)不会被忽略
    .ignoreHttpErrors(true)//即使返回的`HttpStatus`不在200~400之间时也不抛出异常
    .charset("GBK")//使用GBK编码
    .contentType("application/x-www-form-urlencoded")
    .accept("application/json")
    .userAgent("Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0")
    .data("ID", null)//因为设置了*不忽略空参数*,所以会生成"&ID="到url后面(GET)或报文里(POST)
    .data("LOGIN_NO", loginNo)
    .signer(signer)//追加签名参数, signer必须实现`org.terramagnet.http.signature.Signer`接口
    .post();

更多配置项请参考[JavaDoc].

  1. 上传文件
InputStream is = null;
try {
    is = new FileInputStream("C:\\Users\\Administrator\\Pictures\\psb.png");
    HttpFacade.connect("http://....../")
        .data("file", "我的图片1.png", is)
        .post();
} finally {
    if(is!=null){
        is.close();
    }
}

为什么选择 HttpFacade ?

org.slf4j.slf4j-api这个日志依赖外,没有其他第三方依赖。

这个是个比OkHttp还要小的HttpClient

The MIT License (MIT) Copyright (c) 2015 terrason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

一个简单的HTTP客户端 expand collapse
Java
MIT
Cancel

Releases

No release

Contributors

All

Activities

Load More
can not load any more
Java
1
https://gitee.com/terrason/HttpFacade.git
git@gitee.com:terrason/HttpFacade.git
terrason
HttpFacade
HttpFacade
master

Search