package com.fenxiao.channel.quxun;
import java.io.IOException;
import java.util.Map;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import javax.xml.ws.http.HTTPException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class QuXunFlowTest01 {
public static void main(String[] args) {
//order(); //下单
query(); //查询
}
@SuppressWarnings({ "deprecation", "rawtypes", "unused" })
private static void order() {
// TODO Auto-generated method stub
String contract_id ="100001"; //合同ID
String facevalue = "1"; //订单面值
String order_id = "11100000112233"; //订单ID
String plat_offer_id = "TBC00000050B"; //销售品ID
String phone_id = "17767213541"; //手机号码
String request_no = "Q123123123123111"; //请求编号
String partner_no = "700248"; //参数
String key = "LHbZqppfqD4kt9ur"; //密钥参数
String vi = "5832512899616778"; //密钥参数
String testurl = "http://test.e7chong.com:8899/data/order";//下单地址
// String testurl = "http://test.e7chong.com:8899/data/query";//查询地址
String timestamp = System.currentTimeMillis()+""; //时间戳
JSONObject json = new JSONObject();
/*
* 订购流量
*/
json.put("contract_id", contract_id);
json.put("facevalue", facevalue);
json.put("order_id", order_id);
json.put("plat_offer_id", plat_offer_id);
json.put("phone_id", phone_id);
json.put("request_no", request_no);
json.put("timestamp", timestamp);
try {
System.out.println(json.toString());
String code = Encrypt(json.toString(),key,vi);
System.out.println("+++++"+code);
json.clear();
json.put("parner_no", partner_no);
json.put("code", code);
//1、构造HttpClient的实例
HttpClient httpClient = new HttpClient();
//2、创建POST方法的实例
PostMethod postMethod = new PostMethod(testurl);
//3、使用系统提供的默认的恢复策略
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
System.out.println("-------------------");
System.out.println(json.toString());
System.out.println("-------------------");
//4、将表单的值放入postMethod中
postMethod.setRequestBody(json.toString());
try {
//5、执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
System.out.println("statusCode"+statusCode);
if (statusCode!=HttpStatus.SC_OK) {
System.out.println("Method faid:"+postMethod.getStatusLine());
}
//6、读取返回内容
byte[] resonseBody = postMethod.getResponseBody();
//7、处理内容
System.out.println("111111111"+new String(resonseBody));
JSONObject json1 = (JSONObject) JSON.parse(new String(resonseBody));
Map map = (Map)json1;
System.out.println("222222222"+map);
} catch (HTTPException e) {
//发生致命的异常,可能是协议不对或者返回的内容有问题
System.out.println("33333"+"----SimpleDemoPost---postMethod----ex"+e.getMessage());
e.printStackTrace();
}catch (IOException e) {
//发生网络异常
System.out.println("44444"+"----SimpleDemoPost---postMethod----ex"+e.getMessage());
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
@SuppressWarnings({ "deprecation" })
private static void query() {
String partner_no="700248";
String request_no="11100000112233";
String contract_id="100001";
String queryUrl = "http://test.e7chong.com:8899/data/query";
JSONObject json = new JSONObject();
json.put("partner_no", partner_no);
json.put("request_no", request_no);
json.put("contract_id", contract_id);
//1、创建httpclient实例
HttpClient httpClient = new HttpClient();
//2、创建post实例
PostMethod postMethod = new PostMethod(queryUrl);
//3、使用系统默认的恢复策略
postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
System.out.println("-------------------");
System.out.println(json.toString());
System.out.println("-------------------");
//4、将表单放入postMethod中
postMethod.setRequestBody(json.toString());
try {
//5、执行postMethod
int statusCode = httpClient.executeMethod(postMethod);
System.out.println("statusode"+ statusCode);
if (statusCode != HttpStatus.SC_OK ) {
System.out.println("Method failed: " + postMethod.getStatusLine());
}
//6、读取返回内容
byte[] responseBody = postMethod.getResponseBody();
//7、处理内容
System.out.println(new String(responseBody));
} catch (Exception e) {
// TODO: handle exception
}
}
private static String Encrypt(String input, String key, String vi) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"),new IvParameterSpec(vi.getBytes()));
byte[] encrypted = cipher.doFinal(input.getBytes("utf-8"));
//此处使用BASE64做转码
return DatatypeConverter.printBase64Binary(encrypted);
} catch (Exception e) {
return null;
}
}
}