Urlib
使用Java原生库封装一个简单好用的Http请求工具类
Install / Use
/learn @bryonzen/UrlibREADME
示例
- 发送get请求
UrlResp res = Urllib.builder()
.setUrl("http://httpbin.org")
.addPathVariable("get")
.addQuery("keyword", "csdn")
.addHeader("User-Agent", "Chrome")
.addHeader("Content-Type", "text/html")
.addCookies("JSESSIONID=2454;aie=adf")
.addCookie("username", "bin")
.get();
if (res.getResponseCode() == UrlResp.HTTP_OK){
System.out.println(res.getText());
}
打印结果

- 发送post请求并携带表单数据
public void test(){
UrlResp res = Urllib.builder()
.setUrl("http://httpbin.org")
.addPathVariable("post")
.addQuery("keyword", "csdn")
.addHeader("User-Agent", "Chrome")
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.addCookies("JSESSIONID=2454;aie=adf")
.addCookie("yes", "no")
.addPostData("username", "zhangsan")
.addPostData("password", "154134513")
.addPostData("gender", "male")
.addPostData("age", 18)
.post();
if (res.getResponseCode() == UrlResp.HTTP_OK){
System.out.println(res.getText());
}
}
打印结果

- 发送post请求并携带json格式数据
构造json数据稍微复杂一点,毕竟不能像python和php语言。
public void test(){
Map data = new HashMap();
Map keyword1 = new HashMap();
keyword1.put("value", "98gadf");
data.put("keyword1", keyword1);
Map keyword2 = new HashMap();
keyword1.put("value", "9fghfsgdf");
data.put("keyword2", keyword1);
List list = new ArrayList();
list.add("a");
list.add("b");
list.add("c");
UrlResp res = Urllib.builder()
.setUrl("http://httpbin.org")
.addPathVariable("post")
.addHeader("User-Agent", "Chrome")
.addHeader("Content-Type", "application/json")
.addPostData("touser", "orafd98bu")
.addPostData("template_id", "34589u")
.addPostData("page", 1)
.addPostData("form_id", 345)
.addPostData("data", data)
.addPostData("list", list)
.post();
if (res.getResponseCode() == UrlResp.HTTP_OK){
System.out.println(res.getText());
}
}
打印结果

- 保存二进制文件
@Test
public void test() throws IOException {
UrlResp res = Urllib.builder()
.setUrl("https://img-blog.csdnimg.cn/20190324160739729.png")
.addHeader("User-Agent", "Chrome")
.get();
if (res.getResponseCode() == UrlResp.HTTP_OK){
OutputStream os = new BufferedOutputStream(
new FileOutputStream(
new File("photo.png")
)
);
os.write(res.getByteContent());
}
}
输出结果

Related Skills
node-connect
344.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
99.2kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
344.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
344.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
