zoukankan      html  css  js  c++  java
  • post 两种方式 application/x-www-form-urlencoded和multipart/form-data

    本次主要涉及 application/x-www-form-urlencoded方式。

    postman访问方式如图:

     

    java代码实现:

    首先使用maven作为第三方依赖管理:

      

     1    <dependencies>
     2         <dependency>
     3             <groupId>com.alibaba</groupId>
     4             <artifactId>fastjson</artifactId>
     5             <version>1.2.57</version>
     6         </dependency>
     7         <dependency>
     8             <groupId>commons-httpclient</groupId>
     9             <artifactId>commons-httpclient</artifactId>
    10             <version>3.1</version>
    11         </dependency>
    12     </dependencies>

     

    实现的方法块:

     1  public static FullText fullTextSearch(String token, HashMap<String, String> bodyMap) {
     2         headers.put("token", token);
     3 
     4         try {
     5             PostMethod postMethod = new PostMethod(api);   //api为访问的url
     6 
     7             for (Map.Entry<String, String> header : headers.entrySet()) {
     8                 postMethod.setRequestHeader(header.getKey(), header.getValue());
     9             }
    10 
    11             int size = bodyMap.size();
    12             int index = 0;
    13             NameValuePair[]  body = new NameValuePair[size];
    14             for (Map.Entry<String, String> bodyItem : bodyMap.entrySet()) {
    15                 body[index++] = new NameValuePair(bodyItem.getKey(), bodyItem.getValue());
    16             }
    17             postMethod.setRequestBody(body);
    18 
    19             HttpClient httpClient = new HttpClient();
    20             int response = httpClient.executeMethod(postMethod); // 执行POST方法 正常返回200
    21             String result = postMethod.getResponseBodyAsString();
    22             FullText fullText = JSON.parseObject(result, FullText.class);
    23 
    24             return fullText;
    25         } catch (Exception e) {
    26             throw new RuntimeException(e.getMessage());
    27         }
    28     }
  • 相关阅读:
    JUC 1
    给定一个随机数生成器randm(),获得randn()
    堆与优先队列
    集合与映射
    二分搜索树
    链表
    栈与队列
    PostgreSQL ALTER TABLE 命令
    postgresql修改自增序列
    SQL 删除重复行,只保留一条记录
  • 原文地址:https://www.cnblogs.com/dhName/p/11588779.html
Copyright © 2011-2022 走看看