• httpclient之put 方法(参数为json类型)


    出处:https://www.cnblogs.com/jshtest/p/9070711.html

    package com.ftms.didan.info.test;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpStatus;
    import org.apache.http.NameValuePair;
    import org.apache.http.ParseException;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.config.RequestConfig;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.methods.HttpPut;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.util.EntityUtils;
    import org.apache.poi.ss.formula.functions.T;
    import org.jeecg.common.api.vo.Result;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import com.alibaba.fastjson.TypeReference;
    import com.ftms.didan.info.entity.DdInfo;
    
     
    
    public class Test {
    
      public static void main(String[] args) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", "1341637844113121282");
        jsonObject.put("type", "history");
        // jsonObject.put("password", "pdmi1234");
    
        // String url = "http://192.168.1.161:8080/didan/info/ddInfo/list/queryPageDdInfoPublish?pageNo=1&pageSize=10";
        // String url = "http://192.168.1.161:8080/didan/version/ddVersion/queryByVerstionType?type=android";
        String url = "http://192.168.1.161:8080/didan/version/ddVersion/setVersion";
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        // 配置超时时间
        RequestConfig requestConfig = RequestConfig.custom()
          .setConnectTimeout(5000)
          .setConnectionRequestTimeout(5000)
          .setRedirectsEnabled(true)
          .build();
    
        HttpPut httpPost = new HttpPut(url);
          //设置超时时间
          httpPost.setConfig(requestConfig);
          httpPost.setHeader("Content-Type", "application/json");
          httpPost.setHeader("X-Access-Token",       "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MDg3MjM1MDYsInVzZXJuYW1lIjoiZnRtc2FkbWluIn0.kAonFuVKswsrhAJI4VcrG9WPncYj6-        WMmKsLQUW98II");
    
      try {
        StringEntity entity = new StringEntity(jsonObject.toString(), "utf-8");
          entity.setContentEncoding("utf-8");
          entity.setContentType("application/json");
        httpPost.setEntity(entity);
    
        //执行post
        CloseableHttpResponse closeableHttpResponse = closeableHttpClient.execute(httpPost);
        String strRequest = "";
          if (null != closeableHttpResponse && !"".equals(closeableHttpResponse)) {
            System.out.println(closeableHttpResponse.getStatusLine().getStatusCode());
           if (closeableHttpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            HttpEntity httpEntity = closeableHttpResponse.getEntity();
            strRequest = EntityUtils.toString(httpEntity);
            System.out.println(strRequest);
            // 把字符串转换为json对象
            // JSONObject json = JSON.parseObject(strRequest);
            // JSONObject result = json.getJSONObject("result");
            // JSONArray arr = result.getJSONArray("records");
            // List<DdInfo> errors = JSON.parseObject(arr.toJSONString(), new TypeReference<List<DdInfo>>() {});
            // System.out.println(json);
           } else {
            strRequest = "Error Response" + closeableHttpResponse.getStatusLine().getStatusCode();
              }
             }
    
      } catch (ClientProtocolException e) {
        e.printStackTrace();
      } catch (ParseException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        try {
         if (closeableHttpClient != null) {
          closeableHttpClient.close();
            }
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    
    }
  • 相关阅读:
    通用权限管理设计 之 数据库结构设计 [转载]
    RBAC权限管理模型(转)
    C# ReportViewer报表 详解
    人生两支笔
    用C#编写ActiveX控件(一)
    SQL操作全集
    用C#编写ActiveX控件(二)
    Attribute在.NET编程中的应用(三)
    Attribute在.NET编程中的应用(四)
    winform自动升级
  • 原文地址:https://www.cnblogs.com/Bkxk/p/14180690.html
Copyright © 2020-2023  润新知