欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何使用HttpClient發(fā)送java對象到服務器

 更新時間:2019年11月12日 14:35:51   作者:這很周銳  
這篇文章主要介紹了如何使用HttpClient發(fā)送java對象到服務器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

這篇文章主要介紹了如何使用HttpClient發(fā)送java對象到服務器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

 一、首先導入apache依賴的pom文件包

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
</dependency>

二、創(chuàng)建JavaBean實體類對象

public class FulFillMent implements BaseModel {
  /**
   * shopify內部訂單物理位置ID
   */
  private Long location_id;
  /**
   * 運單號
   */
  private String tracking_number;
  /**
   * 快遞公司
   */
  private String tracking_company;
  /**
   * shopify平臺內部商品id
   */
  private List<LineItem> line_items;
  public Long getLocation_id() {
    return location_id;
  }
  public void setLocation_id(Long location_id) {
    this.location_id = location_id;
  }
  public String getTracking_number() {
    return tracking_number;
  }

  public void setTracking_number(String tracking_number) {
    this.tracking_number = tracking_number;
  }

  public String getTracking_company() {
    return tracking_company;
  }

  public void setTracking_company(String tracking_company) {
    this.tracking_company = tracking_company;
  }
  public List<LineItem> getLine_items() {
    return line_items;
  }
  public void setLine_items(List<LineItem> line_items) {
    this.line_items = line_items;
  }
}

三、這里封裝一個上傳到服務器的Utils工具類

package com.glbpay.ivs.common.util.https;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;

import java.io.InputStream;
import java.io.OutputStream;
import org.apache.http.client.ClientProtocolException;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class NewHttpClient {
  public static String doPost(String url, Object myclass) {
    CloseableHttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost posturl = new HttpPost(url);
    String result = null;
    String jsonSting = JSON.toJSONString(myclass);
    StringEntity entity = new StringEntity(jsonSting, "UTF-8");
    posturl.setEntity(entity);
    posturl.setHeader("Content-Type", "application/json;charset=utf8");
    // 響應模型
    CloseableHttpResponse response = null;
    try {
      // 由客戶端執(zhí)行(發(fā)送)Post請求
+6      response = httpClient.execute(posturl);
      // 從響應模型中獲取響應實體
      HttpEntity responseEntity = response.getEntity();

      System.out.println("響應狀態(tài)為:" + response.getStatusLine());
      if (responseEntity != null) {
        System.out.println("響應內容長度為:" + responseEntity.getContentLength());
        System.out.println("響應內容為:" + EntityUtils.toString(responseEntity));
        return EntityUtils.toString(responseEntity);
      }
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        // 釋放資源
        if (httpClient != null) {
          httpClient.close();
        }
        if (response != null) {
          response.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
   return null;
  }
  public static String dourl(String url,Object clzz){
    try {
      String jsonString = JSON.toJSONString(clzz);
      URL url1 = new URL(url);
      HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
      //設置允許輸出
      conn.setDoOutput(true);
      //設置允許輸入
      conn.setDoInput(true);
      //設置不用緩存
      conn.setUseCaches(false);
      conn.setRequestMethod("POST");

      //設置傳遞方式
      conn.setRequestProperty("contentType", "application/json");
      // 設置維持長連接
      conn.setRequestProperty("Connection", "Keep-Alive");
      // 設置文件字符集:
      conn.setRequestProperty("Charset", "UTF-8");
      //開始請求
      byte[] bytes = jsonString.toString().getBytes();
      //寫流
      OutputStream stream = conn.getOutputStream();
      stream.write(bytes);
      stream.flush();
      stream.close();
      int resultCode=conn.getResponseCode();
       if(conn.getResponseCode()==200){
       InputStream inputStream = conn.getInputStream();
       byte[] bytes1 = new byte[inputStream.available()];
       inputStream.read(bytes1);
       //轉字符串
       String s = new String(bytes);
       System.out.println(s);
         return s;
     }else {
         //獲取響應內容
         int code = conn.getResponseCode();
         String responseMessage = conn.getResponseMessage();
         System.out.println(code);
         String s = String.valueOf(code);
         return "響應狀態(tài)碼是:"+s+"響應內容是:======="+responseMessage;
       }
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
         return null;
  }

}

開始調用

FulFillMentModel fulFillMentModel = new FulFillMentModel();
FulFillMent fulfillment = new FulFillMent();
fulfillment.setLocation_id(order.getLocationId());
fulfillment.setTracking_number(order.getTrackingNo());
fulfillment.setTracking_company(order.getChannelCode());
fulfillment.setLine_items(lineItemList);
fulFillMentModel.setFulfillment(fulfillment);
String url = String.format("https://%s:%s@stuushop-com.myshopify.com/admin/api/2019-07/orders/%s/fulfillments.json", settingModel.getAppKey(), settingModel.getPassword(), order.getLastOrderId());
logger.info("shopify平臺請求地址:{},請求數(shù)據(jù):{}", url, JsonUtils.bean2json(fulFillMentModel));
String s = NewHttpClient.doPost(url,fulFillMentModel);
logger.info("shopify平臺返回數(shù)據(jù):{}", JsonUtils.bean2json(s));

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Spring Cloud Gateway重試機制的實現(xiàn)

    Spring Cloud Gateway重試機制的實現(xiàn)

    這篇文章主要介紹了Spring Cloud Gateway重試機制的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Java多線程之悲觀鎖與樂觀鎖

    Java多線程之悲觀鎖與樂觀鎖

    這篇文章主要為大家詳細介紹了Java悲觀鎖與樂觀鎖,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • java中文傳值亂碼問題的解決方法

    java中文傳值亂碼問題的解決方法

    這篇文章主要為大家詳細介紹了java中文傳值亂碼問題的解決方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Java如何使用ConfigurationProperties獲取yml中的配置

    Java如何使用ConfigurationProperties獲取yml中的配置

    這篇文章主要介紹了Java如何使用ConfigurationProperties獲取yml中的配置,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java線程同步的四種方式詳解

    Java線程同步的四種方式詳解

    這篇文章主要介紹了Java線程同步的四種方式詳解,需要的朋友可以參考下
    2023-02-02
  • 什么是jsoup及jsoup的使用

    什么是jsoup及jsoup的使用

    jsoup是一款基于Java的HTML解析器,它提供了一套非常省力的API,不但能直接解析某個URL地址、HTML文本內容,而且還能通過類似于DOM、CSS或者jQuery的方法來操作數(shù)據(jù),所以?jsoup?也可以被當做爬蟲工具使用,這篇文章主要介紹了什么是jsoup及jsoup的使用,需要的朋友可以參考下
    2023-10-10
  • InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別

    InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別

    這篇文章主要介紹了InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別的相關資料,需要的朋友可以參考下
    2015-12-12
  • Java的Dialog和FileDialog你知道啊

    Java的Dialog和FileDialog你知道啊

    這篇文章主要為大家詳細介紹了Java的Dialog和FileDialog,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • Java Web會話技術Session的簡單使用

    Java Web會話技術Session的簡單使用

    在請求需要傳遞的信息比較多,使用Cookie技術就會增大請求的難度。而Session可以存儲對象、數(shù)組等信息,并且Session是存儲到服務器端的,在客戶端請求時只需要將session id一并攜帶給服務器端。本文將簡單的介紹如何使用Session
    2021-05-05
  • java面試try-with-resources問題解答

    java面試try-with-resources問題解答

    這篇文章主要介紹了java面試try-with-resources問題解答,?這個語句的作用是,確保該語句執(zhí)行之后,關閉每一個資源,也就是說它確保了每個資源都在生命周期結束之后被關閉
    2022-07-07

最新評論