1. 程式人生 > 其它 >java中http協議呼叫get請求

java中http協議呼叫get請求

package com.bjbn.app.tianfu.mq.util;



import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


/**
* Date: 2021/12/27:15:49 <br/>
* Description:
*/
public class GetMessage {

public static String getHttpRequestData(String data) {


// 首先抓取異常並處理
String returnString = "1";
try{
// 程式碼實現以GET請求方式為主,POST跳過
/** 1 GET方式請求資料 start*/
StringBuilder sb = new StringBuilder();
String urlP = "http://localhost:8500/main/dataAnalysis?data=";
sb.append(urlP);
data = URLEncoder.encode(data,"UTF-8");
sb.append(data);


// 1 建立URL物件,接收使用者傳遞訪問地址物件連結
URL url = new URL(sb.toString());

// 2 開啟使用者傳遞URL引數地址
HttpURLConnection connect = (HttpURLConnection) url.openConnection();

// 3 設定HTTP請求的一些引數資訊
connect.setRequestMethod("GET"); // 引數必須大寫
connect.connect();


// 4 獲取URL請求到的資料,並建立資料流接收
InputStream isString = connect.getInputStream();

// 5 構建一個字元流緩衝物件,承載URL讀取到的資料
BufferedReader isRead = new BufferedReader(new InputStreamReader(isString));

// 6 輸出列印獲取到的檔案流
String str = "";
while ((str = isRead.readLine()) != null) {
str = new String(str.getBytes(),"UTF-8"); //解決中文亂碼問題
// System.out.println("檔案解析列印:");
// System.out.println(str);
returnString = str;
}

// 7 關閉流
isString.close();
connect.disconnect();

// 8 JSON轉List物件
// do somthings

}catch(Exception e){
e.printStackTrace();
}

return returnString;
}

public static void main(String[] args) {
String data = "{\"dataStatus\":\"新增\",\"dataInfo\":{\"id\":196745,\"masterId\":583,\"assetInId\":\"\",\"assetNum\":\"010202-7\",\"assetName\":\"防車站1\",\"supplierid\":\"\",\"suppliername\":\"\",\"brand\":\"/\",\"model\":\"/\",\"assetBuyId\":0,\"assetBuyTitle\":\"\",\"checkTime\":\"2022-03-04\",\"assetType\":\"2010202\",\"assetTypeName\":\"消防工程\",\"belongOrganId\":\"14776\",\"belongOrganName\":\"成都天府國際機場分公司\",\"useOrganId\":\"14776\",\"useOrganName\":\"成都天府國際機場分公司\",\"mDeptId\":\"15076\",\"mDeptName\":\"資產管理部\",\"useDeptId\":\"15053\",\"useDeptName\":\"辦公室\",\"useDepartId\":\"15054\",\"useDepartName\":\"行政專案\",\"useUserId\":\"\",\"useUserName\":\"共用\",\"installPlace\":\"行政專案\",\"assetLevel\":\"3\",\"assetStatus\":\"1\",\"workTime\":\"2022-03-04\",\"belongSys\":\"\",\"guarantee\":\"2023-03-04\",\"isUpkeep\":\"0\",\"period\":\"\",\"lastUpkeepTime\":\"\",\"nextUpKeepTime\":\"\",\"assetPrice\":\"500000.00\",\"useYear\":300,\"usedmonth\":0,\"leftmonth\":300,\"netvalue\":\"\",\"accudep\":\"\",\"entryTime\":\"2022-03-04\",\"useStatusName\":\"固定資產生產用\",\"addTypeId\":\"\",\"addTypeName\":\"\",\"cwId\":\"131101_08\",\"cwName\":\"資產管理部\",\"ratioOfRemain\":\"0.03\",\"fwxm\":\"\",\"plateNum\":\"\",\"VIN\":\"\",\"motor\":\"\",\"gzfax\":\"\",\"fuel\":\"\",\"outVol\":\"\",\"emsStand\":\"\",\"nextInsureTime\":\"\",\"landCer\":\"\",\"landArea\":\"\",\"landUse\":\"\",\"landXz\":\"\",\"landAddr\":\"\",\"houseCer\":\"\",\"houseArea\":\"\",\"houseFrame\":\"\",\"houseLays\":\"\",\"houseAddr\":\"\",\"remark\":\"\",\"isOfficial\":\"1\",\"isFix\":\"1\",\"isDel\":\"0\",\"rfidNum\":\"202203040006\",\"statusTime\":\"\",\"opUserId\":6647,\"opUserName\":\"馬海若\",\"opTime\":\"2022-03-04\",\"useOrganLocation\":\"四川省機場集團有限公司/成都天府國際機場分公司\",\"mDeptLocation\":\"四川省機場集團有限公司/成都天府國際機場分公司/資產管理部\",\"useDeptLocation\":\"四川省機場集團有限公司/成都天府國際機場分公司/辦公室\",\"useDepartLocation\":\"四川省機場集團有限公司/成都天府國際機場分公司/辦公室/行政專案\",\"useStatusCode\":\"0101\",\"fwxmCode\":\"\",\"attrType\":\"\",\"financeCode\":\"0102\",\"trdCode\":\"H1-03\",\"bearfees\":\"\",\"belongOrganCode\":\"1101\",\"accCode\":\"1101-0001\",\"useDeptCode\":\"1101_04\",\"cwCode\":\"1101_08\",\"fillStatus\":\"1\",\"mDeptCode\":\"1101_08\",\"useDepartCode\":\"1101_04\",\"hasEntry\":\"\",\"isLocAsset\":\"0\",\"placeCode\":\"\",\"longitude\":\"\",\"latitude\":\"\"}}";
String httpRequestData = getHttpRequestData(data);
System.out.println(httpRequestData);

}
}