SpringBoot2.1.x,okhttp3网络请求之GET、POST请求

更新时间:2023-05-14 08:53:48 阅读: 评论:0

SpringBoot2.1.x,okhttp3⽹络请求之GET、POST请求⼀)okhttp3简介
okhttp是⼀个⾼性能的http库,⽀持同步、异步请求,并且实现了spdy、http2、websocket协议,api⽐较简洁易⽤。
核⼼类:
OkHttpClient:⽤于初始化http请求信息
Request:请求参数信息
Call:回调函数信息
RequestBody:请求报⽂信息
Respon:请求响应信息
http请求⽅式区别:
multipart/form-data:该⽅式可以⽤键值对传递参数,并且可以上传⽂件。
application/x-www-form-urlencoded:该⽅式只能以键值对传递参数,并且参数最终会拼接成⼀条,⽤“&”符合隔开。application/json:该⽅式是以对象的⽅式传递参数,对象中的信息以键值对传递。
⼆)okhttp3案例
第⼀步:创建⼀个maven项⽬,引⼊springboot的jar、再引⼊okhttp3的jar
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance" xsi:schemaLocation="maven.apach    <modelVersion>4.0.0</modelVersion>
<groupId&pt</groupId>
<artifactId>springboot_okhttp3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/>
</parent>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
学写传记
综合执法大队
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass&pt.Okhttp3Application</mainClass>
</configuration>
</plugin>
<plugin>那一夜我们
<groupId>at.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
创建l配置⽂件,设置访问端⼝
rver:
port: 8080
创建springboot启动类:偶书古诗
pt;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Okhttp3Application {
public static void main(String[] args) {
SpringApplication.run(Okhttp3Application.class, args);
}
}
创建⼀个JavaBean,⽤于参数传递
pt.vo;
public class FileVO {
private String fileName;
血脉通胶囊private String fileContent;
private Integer fileSize;
private String fileType;
public FileVO() {}
public FileVO(String fileName, String fileContent, Integer fileSize, String fileType) {
this.fileName = fileName;
this.fileContent = fileContent;
this.fileSize = fileSize;
this.fileType = fileType;
}
public String getFileName() {return fileName;}
public void tFileName(String fileName) {this.fileName = fileName;}
public String getFileContent() {return fileContent;}
public void tFileContent(String fileContent) {this.fileContent = fileContent;}
public Integer getFileSize() {return fileSize;}
public void tFileSize(Integer fileSize) {this.fileSize = fileSize;}
public String getFileType() {return fileType;}
public void tFileType(String fileType) {this.fileType = fileType;}
public String toString() {
return "DataVO[fileName: "+this.fileName+", fileSize: "+this.fileSize+", fileType: "+this.fileType+"]";    }
}
第⼆步:创建⼀个GET请求Controller类,提供⼀些测试接⼝
pt.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
pt.vo.FileVO;
@RestController
public class GetController {
/**
* 访问地址: localhost:8080/okhttp3/get/params
* @return
*/
@RequestMapping(value="/okhttp3/get/params", method = RequestMethod.GET)
public String getParams(@RequestParam(value = "param1") String param1, @RequestParam("para
m2") String param2) {        System.out.println("==>/okhttp3/get/params, param1: " + param1 + ", param2: " + param2);
// 处理业务逻辑
return "/okhttp3/get/params SUCCESS!";
}
/**
* 访问地址: localhost:8080/okhttp3/get/form
* @return
*/
@RequestMapping(value="/okhttp3/get/form", method = RequestMethod.GET)
public String getForm(FileVO fileVO) {
业务流程
System.out.println("==>/okhttp3/get/form, fileVO: " + fileVO);
// 处理业务逻辑
return "/okhttp3/get/form SUCCESS";文明礼仪黑板报
}
}
1、GET带参数请求
main⽅法测试:
pt.test;
import java.io.IOException;
import urrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Respon;
/**
* GET带参请求
* @author ouyangjun
*/
public class GetParamsTest {
public static void main(String[] args) {
// 请求地址
String url = "localhost:8080/okhttp3/get/params?param1=EEE¶m2=PPP";
OkHttpClient client = new OkHttpClient()
.newBuilder()
.
connectTimeout(10, TimeUnit.SECONDS) // 设置超时时间
.readTimeout(10, TimeUnit.SECONDS) // 设置读取超时时间
.writeTimeout(10, TimeUnit.SECONDS) // 设置写⼊超时时间
.build();
Request request = new Request.Builder()
早蝉.url(url)
.build();
try (Respon respon = wCall(request).execute()) {
System.out.println("/okhttp3/get 返回结果: " + respon.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Postman测试:
2、GET application/x-www-form-urlencoded表单请求
main⽅法测试:

本文发布于:2023-05-14 08:53:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/896467.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:请求   参数   信息
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图