okhttpclient请求方式
时间: 2023-11-15 17:53:51 浏览: 79
okhttp请求框架
OkHttpClient可以使用多种方式发送HTTP请求。其中一些常见的请求方法包括:
1. 使用GET方法发送请求:
```
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
```
2. 使用POST方法发送请求:
```
RequestBody requestBody = new FormBody.Builder()
.add("key1", "value1")
.add("key2", "value2")
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
```
3. 使用PUT方法发送请求:
```
RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), "request body");
Request request = new Request.Builder()
.url(url)
.put(requestBody)
.build();
Response response = client.newCall(request).execute();
```
4. 使用DELETE方法发送请求:
```
Request request = new Request.Builder()
.url(url)
.delete()
.build();
Response response = client.newCall(request).execute();
```
这些只是一些常见的请求方法示例,OkHttpClient还支持其他类型的请求方法,如HEAD、OPTIONS等。你可以根据需求选择合适的请求方法来发送HTTP请求。
阅读全文