java HTTP代码示例

时间:2022-12-22 19:44:16
//测试环境发送用例
@Test
public void testSendForTest() {
    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);    
}

数据到