1. 程式人生 > >java HTTP代碼示例

java HTTP代碼示例

rac eba object 時間 ati mes highlight bytes sign

//測試環境發送用例 @Test public void testSendForTest() { String url = "http://172.16.30.108:8138/api/doDeliver"; String appkey = "00d40313b7f340ec8df6a3dff293d45c"; String token = "871c69137b654c07b18191859d8bd257b9e092ac1f704336999a2a184a7d9910"; Map<String, String> params = new LinkedHashMap<>();
params.put("appkey", appkey); params.put("p0", "PDS");//業務系統 params.put("p1", "PDS-test");//業務類型 params.put("p2", UUID.randomUUID().toString());//業務id params.put("p3", "STATION_EUAE,STATION_SZAE");//目的站 params.put("p4", "我是測試");//消息內容 params.put("p5", "test");//跟蹤ID params.put("p9"
, "123");//消息內容加密密鑰 String timestamp = System.currentTimeMillis()+""; params.put("timestamp", timestamp);//時間戳 StringBuilder s = new StringBuilder(); for (Iterator<Entry<String, String>> iterator = params.entrySet().iterator(); iterator.hasNext();) { Entry<String, String> entry = iterator.next();
if(StringUtils.isNotBlank(entry.getValue())) { s.append(entry.getKey()).append("=").append(entry.getValue()).append("&"); } } String paramsStr = s.toString(); String signStr = CodecUtil.md5AndBase64(paramsStr.substring(0, paramsStr.length() - 1) + token); params.put("signStr", signStr); String response = HttpUtils.postJson(url, params); System.out.println(response); ResponseMessage responseMessage = JSON.parseObject(response, ResponseMessage.class); System.out.println(responseMessage); }
//簽名代碼 public static String md5AndBase64(String stringValue) { try { byte[] btInput = stringValue.getBytes(Charset.forName("UTF-8")); // 獲得MD5摘要算法的 MessageDigest 對象 MessageDigest mdInst = MessageDigest.getInstance("MD5"); // 使用指定的字節更新摘要 mdInst.update(btInput); // 獲得密文 byte[] md = mdInst.digest(); return Base64.encodeBase64String(md); } catch (Exception e) { logger.error(e.getMessage(), e); throw new IllegalArgumentException(e.getMessage()); } }
//報文示例 POST http://172.16.30.108:8138/api/doDeliver POST data: { "appkey":"00d40313b7f340ec8df6a3dff293d45c", "p0":"PDS", "p1":"PDS-test", "p2":"d84321b2-49a4-4d0e-8783-4fc400df4760", "p3":"STATION_EUAE,STATION_SZAE", "p4":"我是測試", "p5":"test", "p9":"123", "timestamp":"1528877765741", "signStr":"L4c8LWrbHe1U7CIBA/AWfg==" } [no cookies] Request Headers: Connection: close content-type: application/json Content-Length: 290 Host: 172.16.30.108:8138 User-Agent: Apache-HttpClient/4.5.5 (Java/1.8.0_151)

使用說明:

  • 客戶端jar包引入

<dependency> <groupId>com.fpx.sync</groupId> <artifactId>fpx-sync-client</artifactId> <version>1.1.1-SNAPSHOT</version> </dependency>
  • 引入客戶端配置文件或自行添加配置項

引入配置文件註入客戶端配置數據 <import resource="classpath*:spring-sync-client.xml" /> 或者自行添加配置項註入 <bean class="com.fpx.sync.client.deliver.util.ConfigUtils"> <!-- 傳送站同步地址 --> <property name="stationSyncHost" value="172.16.30.108"></property> <!-- 傳送站同步端口 --> <property name="stationSyncPort" value="33306"></property> <!-- 傳送站同步文件路徑 --> <property name="stationSyncFileBasePath" value="/mnt/sync"></property> <!-- 傳送服務登錄秘鑰 --> <property name="stationDeliverServerLoginPwd" value="B6C3BCCE61DE2A68BE1BF58C5DFDEC9D"></property> <!-- 壓縮包的文件擴展名 --> <property name="compressExtention" value="war,apk,rar,zip,tar,iso,gzip,7z,tar.gz"></property> </bean>

  • 配置properties中添加配置項

#客戶端連接地址 CLIENT_STATION_SYNC_HOST=127.0.0.1 #客戶端連接端口 CLIENT_STATION_SYNC_PORT=33301 #傳送站文件存儲路徑 CLIENT_STATION_SYNC_FILE_BASE_PATH=/mnt/sync/ #傳送站程序交互登錄秘鑰 CLIENT_STATION_DELIVER_SERVER_LOGIN_PWD=B6C3BCCE61DE2A68BE1BF58C5DFDEC9D #壓縮文件類型 COMPRESS_EXTENTION=war,apk,rar,zip,tar,iso,gzip,7z,tar.gz

  • 客戶端使用示例代碼

/** * 測試從深圳中心 發往 美西倉倉庫 * 業務類型需要定義 * 業務Id不能重復 * * @throws IOException */ @Test public void testAreaToWarehouse() throws IOException{ long start = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHHmmSS"); String now = sdf.format(new Date()); String businessSystem = "GRS"; String trackId = "TR0001"; String bizType = "test-delivery-" + now; String bizId = now; String message = UUID.randomUUID().toString(); SyncResult result = DeliverApi.deliverMessage(businessSystem, trackId, bizType, bizId, "STATION_SZAE", "STATION_USLAXA", message); //發送文件 //String filePath = "D:/1.text"; //SyncResult result = DeliverApi.deliverMessage(businessSystem, trackId, bizType, bizId, "STATION_SZAE", "STATION_USLAXA", message, filePath); System.out.println(result.getResult()); long end = System.currentTimeMillis(); System.out.println("耗時 " + (end - start) / 1000 + " 秒"); System.out.println(result.toString()); System.exit(0); }

數據到

java HTTP代碼示例