7.16Java接口之关于ContentType中application/xwwwformurlencoded和multipart/formdata的区别

7.16Java接口之关于Content-Type中application/x-www-form-urlencoded和multipart/form-data的区别

要了解这两个的区别需要从前端的标签说起

表单标签

<form>标签

在Form元素的语法中,EncType表明提交数据的格式 用 Enctype 属性指定将数据回发到服务器时浏览器使用的编码类型

  • application/x-www-form-urlencoded:窗体数据被编码为名称/值对。这是标准的编码格式。

  • multipart/form-data: 窗体数据被编码为一条消息,页上的每个控件对应消息中的一个部分,这个一般文件上传时用。

  • text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。

form的enctype属性为编码方式,常用有两种:

  • application/x-www-form-urlencoded

  • multipart/form-data

默认为application/x-www-form-urlencoded

application/x-www-form-urlencoded

当action为get时候,浏览器用x-www-form-urlencoded的编码方式把form数据转换成一个字串(name1=value1&name2=value2…),然后把这个字串append到url后面,用?分割,加载这个新的url。

它是post的默认格式,使用js中URLencode转码方法。包括将name、value中的空格替换为加号;将非ascii字符做百分号编码;将input的name、value用‘=’连接,不同的input之间用‘&’连接。

同样使用URLencode转码,这种post格式跟get的区别在于,get把转换、拼接完的字符串用‘?’直接与表单的action连接作为URL使用,所以请求体里没有数据;而post把转换、拼接后的字符串放在了请求体里,不会在浏览器的地址栏显示

multipart/form-data

当action为post时候,浏览器把form数据封装到http body中,然后发送到server。

  • 如果没有type=file的控件,用默认的application/x-www-form-urlencoded就可以了

  • 如果有type=file的话,用到multipart/form-data

    • 浏览器会把整个表单以控件为单位分割,并为每个部分加上Content-Disposition(form-data或者file),Content-Type(默认为text/plain),name(控件name)等信息,并加上分割符(boundary)。

    • 每一段------WebKitFormBoundaryh2rFWhmmPZSuKvgf--为对应一部分消息

      enctype="multipart/form-data"是上传二进制数据

多媒体传输的都是大量的数据,所以规定上传文件必须是post方法,<input>的type属性必须是file。form里面的input的值以2进制的方式传过去,所以request就得不到值了。

实例

package OmsOrdersPullInterface;

import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;

/**
* 调用oms接口拉取订单信息实例
* @since JDK 1.8
* @date 2021/07/17
* @author Lucifer
*/
public class omsPullInterface {
   //定义接口地址
   private static final String Url = "";
   //捕获到本次登录的Cookie值
   private static final String Cookie = "";
   //定义参数格式
   private static final String Params = "platform=shopee&platform_accountid=12034&startTime=2021-07-16&endTime=2021-07-17&orderid=";

   /**
    *用HttpClient类下的方法创建POST请求demo
    * Content-Type:x-www-form-urlencoded
    */
   public static void doPullOrders(String url, String params){
       //创建Cookie存储
       CookieStore cookieStore = new BasicCookieStore();
       //设置Cookie值
       BasicClientCookie cookie = new BasicClientCookie("Cookie", Cookie);
       //设置域
       cookie.setDomain("");
       //设置路径
       cookie.setPath("/");
       //将设置好的Cookie放入接口引用中
       cookieStore.addCookie(cookie);
       //使用HttpClient创建登录客户端
       CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
       //创建响应模型,通过请求对象获取响应对象--->发送登录请求
       CloseableHttpResponse response = null;

       /*创建请求实体*/
       try{
           //创建登录方式引用
           HttpPost httpPost = new HttpPost(url);
           //设置请求配置
           RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(3000).setConnectionRequestTimeout(3000).setSocketTimeout(3000).build();
           //设置POST请求配置
           httpPost.setConfig(requestConfig);
           //添加请求头
           httpPost.addHeader("Content-Type", "x-www-form-urlencoded; charset=utf-8");
           //设置接收形式
           httpPost.setHeader("Accept", "application/json");
           System.out.println(httpPost);
           //创建登录实体
           httpPost.setEntity(new StringEntity(params, Charset.forName("UTF-8")));

           System.out.println(httpPost);
           //判断请求实体情况
           if (httpPost == null){
               System.out.println("请求实体为空!");
               return;
          }else {
               response = httpClient.execute(httpPost);
          }
      }catch (Exception e){
           System.out.println(e.getMessage());
           e.printStackTrace();
      }

       /*创建响应实体*/
       try {
           //从响应模型中获得响应实体
           HttpEntity responseEntity = response.getEntity();
           if (responseEntity == null){
               System.out.println("响应实体对象为空!");
               throw new IOException();
          }else {
               //打印响应状态
               System.out.println("响应状态为:" + responseEntity.getContentType());
               System.out.println("响应内容长度为:" + responseEntity.getContentLength());
               System.out.println("响应内容为:" + EntityUtils.toString(responseEntity));
          }
      }catch (Exception e){
           System.out.println(e.getMessage());
           e.printStackTrace();
      }finally {
           //关闭资源
           try {
               //判断客户端实体
               if (httpClient != null) {
                   httpClient.close();
              }
               //判断响应实体
               if (response != null) {
                   response.close();
              }
          }catch (Exception e){
               System.out.println(e.getMessage());
               e.printStackTrace();
          }
      }
  }

   public static void main(String[] args) {
       doPullOrders(Url, Params);
  }
}

 

It's a lonely road!!!
原文地址:https://www.cnblogs.com/JunkingBoy/p/15021737.html