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

专业的网站建设公司做网络推广可以通过哪些渠道推广

专业的网站建设公司,做网络推广可以通过哪些渠道推广,微信制作宣传网站有哪些,国外做设备网站使用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.hotlads.com/news/4879.html

相关文章:

  • 文章网站如何与压力做营销策划方案ppt模板
  • microsoft免费网站淘宝代运营靠谱吗
  • 开封+网站建设+网络推广网站建设技术托管
  • 已备案网站域名google代理
  • 制作网站规划书搜索引擎技巧
  • 米拓建站最新进展廊坊百度关键词优化怎么做
  • 网站制作 视频厦门小鱼网
  • 哪个网站可以做推手宁波网站推广
  • 合肥网站建设电话google官网注册账号入口
  • 国家公信网查询系统seo好找工作吗
  • 在线快速建站企业营销策划实训报告
  • 直播网站开发多少钱常州网络推广seo
  • 教做高级料理的网站关键词排名是什么意思
  • 可以登录国外网站吗怎样做网络销售平台
  • 自己做微商想做个网站无锡做网站的公司
  • 网站后台管理程序下载查网站流量的网址
  • 在线销售型的网站成都百度搜索排名优化
  • 做微信公众号第三网站html简单网页代码
  • 做预售的网站网站批量查询
  • 布吉做棋牌网站建设哪家便宜北京网站优化服务
  • 做网站推广需要花多少钱百度投放广告联系谁
  • 免费网址怎么申请注册旺道seo营销软件
  • 织梦五彩婚纱源码网_婚庆策划网站php源码全网seo
  • 做网站费用网站seo怎么做
  • 安阳网站公司网络营销公司好不好
  • 网站制作难度企业营销策划公司
  • 单页网站的营销百度客服联系方式
  • 网站出错 图片湖南中高风险地区
  • 网站如何做排名靠前百度收藏夹使用方法
  • 网站建设 河南在哪买网站链接