当前位置: 首页 > news >正文

专业的网站建设公司品牌战略

专业的网站建设公司,品牌战略,wordpress 主题 试用,网站开发英语使用OkHttp发送POST请求的几种方式 介绍pom依赖基本的POST请求带授权的POST请求POST方式发送JSON数据Multipart POST 请求 介绍 本文将介绍 OkHttp 客户端的基本用法。 主要介绍 OkHttp 3.x 版本中发送Post请求的几种方式。 pom依赖 <dependency><groupId>com.sq…

使用OkHttp发送POST请求的几种方式

    • 介绍
    • pom依赖
    • 基本的POST请求
    • 带授权的POST请求
    • POST方式发送JSON数据
    • Multipart POST 请求

介绍

本文将介绍 OkHttp 客户端的基本用法。
主要介绍 OkHttp 3.x 版本中发送Post请求的几种方式。

pom依赖

        <dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.8.0</version></dependency>

基本的POST请求

使用 FormBody.Builder 构造基本的 RequestBody , 包含两个参数:用户名、密码,发送 POST请求。

    public static void main(String[] args) {String BASE_URL = "http://localhost:8080/okhttp3/test";RequestBody formBody = new FormBody.Builder().add("username", "zhangsan").add("password", "123456").build();Request request = new Request.Builder().url(BASE_URL + "/users").post(formBody).build();Call call = new OkHttpClient().newCall(request);Response response = null;try {response = call.execute();} catch (IOException e) {System.out.println("execute failed, message:" + e.getMessage());}assert response != null;if (!response.isSuccessful()) {System.out.println("request failed");}}

带授权的POST请求

如果要对请求进行身份验证,可以使用 Credentials.basic 构建器向请求头中添加凭据。
下面代码给出发送一个 String 字符串作为请求体带授权的例子:

    public static void main(String[] args) {String BASE_URL = "http://localhost:8080/okhttp3/test";// 带授权的POST请求String postBody = "content";Request request = new Request.Builder().url(BASE_URL + "/users").addHeader("Authorization", Credentials.basic("username", "password")).post(RequestBody.create(MediaType.parse("text/x-markdown"), postBody)).build();Call call = new OkHttpClient().newCall(request);Response response = null;try {response = call.execute();} catch (IOException e) {System.out.println("execute failed, message:" + e.getMessage());}assert response != null;if (!response.isSuccessful()) {System.out.println("request failed");}}

POST方式发送JSON数据

为了在请求体中发送 JSON,我们必须设置它的媒体类型 application/json。 我们可以使用 RequestBody.create构建器来构造:

    public static void main(String[] args) {String BASE_URL = "http://localhost:8080/okhttp3/test";// POST方式发送JSON数据String json = "{\"username\":zhangsan,\"password\":\"123456\"}";RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);Request request = new Request.Builder().url(BASE_URL + "/users").post(body).build();Call call = new OkHttpClient().newCall(request);Response response = null;try {response = call.execute();} catch (IOException e) {System.out.println("execute failed, message:" + e.getMessage());}assert response != null;if (!response.isSuccessful()) {System.out.println("request failed");}}

Multipart POST 请求

我们需要将 RequestBody 构建为一个 MultipartBody 来发布文件、用户名和密码的 POST 请求:

    public static void main(String[] args) {String BASE_URL = "http://localhost:8080/okhttp3/test";// Multipart POST请求RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("username", "zhangsan").addFormDataPart("password", "123456").addFormDataPart("file", "file.txt",RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))).build();Request request = new Request.Builder().url(BASE_URL + "/users/multipart").post(requestBody).build();Call call = new OkHttpClient().newCall(request);Response response = null;try {response = call.execute();} catch (IOException e) {System.out.println("execute failed, message:" + e.getMessage());}assert response != null;if (!response.isSuccessful()) {System.out.println("request failed");}}
http://www.shuangfujiaoyu.com/news/17439.html

相关文章:

  • 拓者设计吧论坛路由优化大师
  • 鞍山网站建设营销网址查询域名解析
  • 客户细分精准营销app优化推广
  • 政府网站推广方案企业网络营销顾问
  • 做外贸必看的网站和论坛有哪些百度贴吧网页版登录入口
  • 局域网怎么建设网站aso优化哪家好
  • 公司网站内容更新该怎么做网站排名软件利搜
  • 化工网站模板下载百度优化是什么意思
  • 做网站需要的流程网站软件推荐
  • 小程序开发软件有哪些百度优化推广
  • 滨海新区商城网站建设网络推广与推广
  • 如何做网站二维码手机搜索引擎
  • 网上注册公司靠谱吗百度seo原理
  • 设计行业网站什么是seo标题优化
  • 网站建设皖icp下载app到手机上并安装
  • wordpress栏目加密搜索引擎优化的目的是
  • 广州做网站信科分公司最新推广注册app拿佣金
  • 西安购物网站建设seo品牌推广方法
  • 惠来做网站湖南专业seo推广
  • 河南城市建设招标类网站最有效的宣传方式
  • 快速做网站软件什么是sem推广
  • 潍坊网站建设联系电话宁波网站建设方案推广
  • 好网站在哪里2345浏览器下载
  • 天津网站优化公司培训网站
  • 关于酒店网站建设的摘要今日最新国际新闻
  • 麻城网站建设play商店
  • 惠州外贸网站建设关键词的作用
  • 做网站需要的合同起飞页自助建站平台
  • 网站交互行为搜索引擎名词解释
  • 做直销建立个人网站好吗今天新疆新闻头条