最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

 一、首先導(dǎo)入apache依賴(lài)的pom文件包

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

二、創(chuàng)建JavaBean實(shí)體類(lèi)對(duì)象

public class FulFillMent implements BaseModel {
  /**
   * shopify內(nèi)部訂單物理位置ID
   */
  private Long location_id;
  /**
   * 運(yùn)單號(hào)
   */
  private String tracking_number;
  /**
   * 快遞公司
   */
  private String tracking_company;
  /**
   * shopify平臺(tái)內(nèi)部商品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;
  }
}

三、這里封裝一個(gè)上傳到服務(wù)器的Utils工具類(lèi)

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");
    // 響應(yīng)模型
    CloseableHttpResponse response = null;
    try {
      // 由客戶(hù)端執(zhí)行(發(fā)送)Post請(qǐng)求
+6      response = httpClient.execute(posturl);
      // 從響應(yīng)模型中獲取響應(yīng)實(shí)體
      HttpEntity responseEntity = response.getEntity();

      System.out.println("響應(yīng)狀態(tài)為:" + response.getStatusLine());
      if (responseEntity != null) {
        System.out.println("響應(yīng)內(nèi)容長(zhǎng)度為:" + responseEntity.getContentLength());
        System.out.println("響應(yīng)內(nèi)容為:" + 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();
      //設(shè)置允許輸出
      conn.setDoOutput(true);
      //設(shè)置允許輸入
      conn.setDoInput(true);
      //設(shè)置不用緩存
      conn.setUseCaches(false);
      conn.setRequestMethod("POST");

      //設(shè)置傳遞方式
      conn.setRequestProperty("contentType", "application/json");
      // 設(shè)置維持長(zhǎng)連接
      conn.setRequestProperty("Connection", "Keep-Alive");
      // 設(shè)置文件字符集:
      conn.setRequestProperty("Charset", "UTF-8");
      //開(kāi)始請(qǐng)求
      byte[] bytes = jsonString.toString().getBytes();
      //寫(xiě)流
      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);
       //轉(zhuǎn)字符串
       String s = new String(bytes);
       System.out.println(s);
         return s;
     }else {
         //獲取響應(yīng)內(nèi)容
         int code = conn.getResponseCode();
         String responseMessage = conn.getResponseMessage();
         System.out.println(code);
         String s = String.valueOf(code);
         return "響應(yīng)狀態(tài)碼是:"+s+"響應(yīng)內(nèi)容是:======="+responseMessage;
       }
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
         return null;
  }

}

開(kāi)始調(diào)用

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平臺(tái)請(qǐng)求地址:{},請(qǐng)求數(shù)據(jù):{}", url, JsonUtils.bean2json(fulFillMentModel));
String s = NewHttpClient.doPost(url,fulFillMentModel);
logger.info("shopify平臺(tái)返回?cái)?shù)據(jù):{}", JsonUtils.bean2json(s));

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java?ArrayList實(shí)現(xiàn)班級(jí)信息管理系統(tǒng)

    Java?ArrayList實(shí)現(xiàn)班級(jí)信息管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java?ArrayList實(shí)現(xiàn)班級(jí)信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • SpringBoot中異常處理實(shí)戰(zhàn)記錄

    SpringBoot中異常處理實(shí)戰(zhàn)記錄

    在我們實(shí)際項(xiàng)目開(kāi)放中經(jīng)常需要我們處理很多的異常,如何在spring boot項(xiàng)目里面實(shí)現(xiàn)異常處理呢,下面這篇文章主要給大家介紹了關(guān)于SpringBoot中異常處理的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • java實(shí)現(xiàn)人民幣大小寫(xiě)轉(zhuǎn)換方法分享

    java實(shí)現(xiàn)人民幣大小寫(xiě)轉(zhuǎn)換方法分享

    本文介紹java人民幣數(shù)字大小寫(xiě)轉(zhuǎn)換方法,代碼中有注釋?zhuān)蠹抑苯涌创a吧
    2014-01-01
  • SpringBoot disruptor高性能隊(duì)列使用

    SpringBoot disruptor高性能隊(duì)列使用

    這篇文章主要介紹了SpringBoot disruptor高性能隊(duì)列使用,Disruptor是英國(guó)外匯交易公司LMAX開(kāi)發(fā)的一個(gè)高性能隊(duì)列,研發(fā)的初衷是解決內(nèi)存隊(duì)列的延遲問(wèn)題
    2023-02-02
  • Java 在線考試云平臺(tái)的實(shí)現(xiàn)

    Java 在線考試云平臺(tái)的實(shí)現(xiàn)

    讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+vue+springboot+mysql+maven實(shí)現(xiàn)一個(gè)前端vue后臺(tái)java微服務(wù)的在線考試系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平
    2021-11-11
  • 用JAVA實(shí)現(xiàn)單鏈表,檢測(cè)字符串是否是回文串

    用JAVA實(shí)現(xiàn)單鏈表,檢測(cè)字符串是否是回文串

    這篇文章主要介紹了使用JAVA實(shí)現(xiàn)單鏈表,檢測(cè)字符串是否是回文串,幫助大家更好的理解和使用Java,感興趣的朋友可以了解下
    2020-11-11
  • IDEA使用Maven創(chuàng)建module出現(xiàn)Ignored?pom.xml問(wèn)題及解決

    IDEA使用Maven創(chuàng)建module出現(xiàn)Ignored?pom.xml問(wèn)題及解決

    這篇文章主要介紹了IDEA使用Maven創(chuàng)建module出現(xiàn)Ignored?pom.xml問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • 詳解Spring Boot 項(xiàng)目部署到heroku爬坑

    詳解Spring Boot 項(xiàng)目部署到heroku爬坑

    這篇文章主要介紹了詳解Spring Boot 項(xiàng)目部署到heroku爬坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-08-08
  • LCN分布式事務(wù)解決方案詳解

    LCN分布式事務(wù)解決方案詳解

    這篇文章主要介紹了LCN分布式事務(wù)解決方案詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Spring計(jì)劃任務(wù)用法實(shí)例詳解

    Spring計(jì)劃任務(wù)用法實(shí)例詳解

    這篇文章主要介紹了Spring計(jì)劃任務(wù)用法,結(jié)合實(shí)例形式詳細(xì)分析了spring計(jì)劃任務(wù)相關(guān)原理、配置、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-11-11

最新評(píng)論

巴马| 华宁县| 南溪县| 石阡县| 罗源县| 明光市| 和平县| 浮山县| 惠州市| 巴林左旗| 古交市| 东安县| 兴安县| 乌拉特中旗| 疏附县| 潞西市| 象山县| 布拖县| 高唐县| 迁安市| 五家渠市| 略阳县| 涿州市| 吉水县| 化隆| 双鸭山市| 乐清市| 弋阳县| 邓州市| 郸城县| 庆安县| 松桃| 阿尔山市| 东乌珠穆沁旗| 六枝特区| 大方县| 陆丰市| 浦江县| 精河县| 沂源县| 曲麻莱县|