net.dongliu.requests

本文介绍Requests库的基础使用方法,包括GET和POST请求、参数传递、设置头部信息等,并提供了文件上传、JSON支持及认证设置的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

requests 是http 请求库,有丰富的API。

git 地址:https://github.com/hsiafan/requests

 

maven 配置:

<dependency>
    <groupId>net.dongliu</groupId>
    <artifactId>requests</artifactId>
    <version>4.18.1</version>
</dependency>

 

基本用法:

  • Get请求,读取response为String
String resp = Requests.get(url).send().readToText()

or

Response<String> resp =  Requests.get(url).send().toTextResponse();

resp包含以下信息:

  • post
String resp = Requests.post(url).send().readToText();
  • 返回对象封装许多元素可以使用
RawResponse rawResponse =  Requests.get("http://localhost:8080/hello/get").send();
int statusCode = rawResponse.getStatusCode();
String contentLen = rawResponse.getHeader("Content-Length");
Cookie cookie = rawResponse.getCookie("Content-Length");
String resp = rawResponse.readToText();
  • 除了readToText还有其他方式消费response
byte[] resp = Requests.get(url).send().readToBytes();
Requests.get(url).send().writeToFile(path);

 

字符集

请求默认的是UTF-8,可以通过以下方式调整字符集

Requests.get(url).requestCharset(StandardCharsets.ISO_8859_1).send().readToText()

返回首先从Header里读取字符集,如果没有则默认UTF-8,如果要用其他字符集读取返回信息,可以使用以下方式指定字符集

Requests.get(url).send().withCharset(StandardCharsets.ISO_8859_1).readToText()

 

传参

Map<String,Object> params = new HashMap<>(1);
params.put("text","hello");
Requests.get(url).params(params).send().readToText();

or

Requests.get(url).params(Parameter.of("text","hello")).send().readToText();

如果要传post www-form-encoded参数,可以用forms方法,不再赘述

 

设置headers

Requests.get(url).headers(new Header("text","hello")).send().readToText()

or

Map<String,Object> headers = new HashMap<>(1);
headers.put("text","hello");
Requests.get(url).headers(headers).send().readToText();

 

同理可以用cookies()设置cookies,不再赘述

还有post 的body()方法,不再赘述

 

如果需要multiPart post进行文件上传

Requests.post(url)
                .multiPartBody(Part.file("test",new File(path)),
                        Part.text("text","hello"))
                .send().readToText();

 

Json支持

Person p = new Person("hello",12);
//发送json body,解析返回json
Requests.post(url).jsonBody(JSON.toJSON(p)).send().readToJson(Person.class);

or

List<Person> persons = Requests.post(url)
                .send().readToJson(new TypeInfer<List<Person>>() {});

 

BasicAuth设置用户名、密码

Requests.get(url).basicAuth(user,password).send()

 

超时设置:

Requests.get(url).socksTimeout(1000*10s).connectTimeout(1000*30).send()

代理设置

Requests.get(url).proxy(Proxies.httpProxy("127.0.0.1", 8081)).send(); // http proxy
Requests.get(url).proxy(Proxies.socksProxy("127.0.0.1", 1080)).send(); // socks proxy proxy

 

 

### 结合Unidbg与apk-parser进行APK解析和调试 #### 导入所需库 为了实现这一目标,首先需要确保项目中已经包含了`unidbg`以及`apk-parser`这两个库。对于Maven构建工具而言,可以在项目的pom.xml文件里加入如下依赖声明: ```xml <dependencies> <!-- Unidbg --> <dependency> <groupId>com.github.unidbg</groupId> <artifactId>unidbg-all</artifactId> <version>0.8.17</version> </dependency> <!-- Apk-parser --> <dependency> <groupId>net.dongliu</groupId> <artifactId>apk-parser</artifactId> <version>2.6.10</version> </dependency> </dependencies> ``` 上述配置使得开发者能够方便地利用两个强大的工具来进行更深入的应用程序分析工作[^4]。 #### 初始化环境并加载APK文件 接下来,在代码层面初始化必要的组件,并尝试读取待处理的目标APK包。这里给出一段简单的Java伪代码作为示范: ```java import com.github.unidbg.Unidbg; import net.dongliu.apk.parser.ApkFile; public class Main { public static void main(String[] args) throws Exception { String apkPath = "path/to/your/app-release.apk"; // 创建Unidbg实例用于模拟执行native层逻辑 final Unidbg unidbg = new Unidbg("/system/lib"); // 加载指定路径下的APK文件到内存中以便后续操作 try (ApkFile apkFile = new ApkFile(new File(apkPath))) { System.out.println("Package Name: " + apkFile.getPackageName()); // 更多基于apk-file的操作... } } } ``` 此部分展示了如何结合两者的优势——即借助于`unidbg`提供的动态仿真能力去探索原生函数行为的同时,也运用到了来自`apk-parser`所提供的静态结构化数据访问接口[^1]。 #### 实现具体功能需求 当完成了基础设置之后,则可以根据实际应用场景定制相应的业务流程。比如想要监控某些敏感API调用情况的话,就可以考虑挂钩(hooking)这些方法;又或是希望提取出资源字符串列表时,就直接遍历Manifest节点下对应的项即可。总之,灵活组合两者的特性往往能带来意想不到的效果。 需要注意的是,虽然理论上讲二者是可以协同工作的,但在实践中可能会遇到诸如版本兼容性等问题。因此建议密切关注官方文档更新和技术社区讨论帖,及时调整方案以适应最新变化趋势[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值