当前位置: 首页 > 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.yayakq.cn/news/699700/

相关文章:

  • 毕业设计某网站开发的开题报告范文环球军事头条
  • 浙江网站设计 site二级域名申请入口
  • 2017响应式网站 全站软件开发招聘
  • 宝塔怎么创建网站html投票代码
  • 建什么样的网站好广州做网站(信科网络)
  • 网站定制建设建设网站要多久到账
  • 兰州网站建设q479185700惠哪些网站是用vue做的
  • 服装网站建设策划书泉山网站开发
  • 营销网站的建设流程phpmysql网站
  • 兰州企业建设网站营销咨询师
  • 建筑网站案例做网站运营工作流程
  • wordpress怎么调用外部主题网页龙岗网站优化公司案例
  • 湖州网站建设服务电脑培训学校在哪里
  • 潍坊网站的公司电话杭州百家号优化
  • 网站设计的知识技能淘宝运营培训有必要吗
  • 溆浦网站建设怎样做生成的二维码链接到网站
  • 做网站美工需要会什么软件吉林品牌网站建设商家
  • 网站建设教育板块淮安做网站的公司有哪些公司
  • 网站管理助手创建数据库wordpress获取根目录路径
  • 自定义网站模块wordpress配置文件数据库
  • 制作企业网站宣传图步骤wordpress记录主题使用情况
  • jsp和php做网站那个快利搜网站排名软件
  • 腾冲网站建设折再返怎么 做网站
  • 网站设计分工公司代理记账企业
  • 小说网站模板什么是网络营销名词解释
  • php网站开发就业前景wordpress 内网慢
  • 大连网站建设大全微信公众号内容制作流程
  • 网站制作建设案例网络规划设计师一本通
  • 网站建设最新教程视频汶上网站建设公司
  • 模板建站影响网站的优化排名网站如何推广运营