import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.Map; public class order {
private static String create_order(String ip, String merchantId, String key, String payWay, String amount)
throws NoSuchAlgorithmException, InvalidKeyException, IOException {
// url参数
Map<String, Object> url_map = new TreeMap<String, Object>();
url_map.put("merchantId", merchantId);
url_map.put("signatureMethod", "HmacSHA256");
url_map.put("signatureVersion", "1");
url_map.put("timestamp", System.currentTimeMillis()); // url参数拼接
StringBuilder url_data = new StringBuilder();
for (Map.Entry<String, Object> url :url_map.entrySet()) {
url_data.append(url.getKey()).append("=").append(url.getValue()).append("&");
}
url_data = new StringBuilder(url_data.substring(0, url_data.length() - 1));
// System.out.println(url_data); // body参数
Map<String, Object> body_map = new TreeMap<String, Object>();
body_map.put("jUserId", "666666");
body_map.put("jUserIp", "110.110.110.110");
body_map.put("jOrderId", Long.toString(System.currentTimeMillis()));
body_map.put("orderType", "1");
body_map.put("payWay", payWay);
body_map.put("amount", amount);
body_map.put("currency", "CNY");
body_map.put("jUserName", "奥里给");
body_map.put("notifyUrl", "https://test.com"); // 全部参数
Map<String, Object> all_map = new TreeMap<String, Object>();
all_map.putAll(url_map);
all_map.putAll(body_map); // 参数拼接
StringBuilder all_data = new StringBuilder();
for (Map.Entry<String, Object> url :all_map.entrySet()) {
all_data.append(url.getKey()).append("=").append(url.getValue()).append("&");
} all_data = new StringBuilder(all_data.substring(0, all_data.length() - 1));
// System.out.println("data:" + all_data); // HmacSHA256加密
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
sha256_HMAC.init(secret_key);
byte[] array = sha256_HMAC.doFinal(all_data.toString().getBytes(StandardCharsets.UTF_8));
StringBuilder sec = new StringBuilder();
for (byte item : array) {
sec.append(Integer.toHexString((item & 0xFF) | 0x100), 1, 3);
}
String signature_data = sec.toString().toUpperCase();
System.out.println("signature:" + signature_data); String URL = ip + "?" + url_data + "&signature=" + signature_data;
System.out.println("url:" + URL);
HttpPost httppost = new HttpPost(URL); // 请求body参数拼接
StringBuilder list_data = new StringBuilder();
for (Map.Entry<String, Object> m :body_map.entrySet()) {
list_data.append(m.getKey()).append("=").append(m.getValue()).append("&");
}
list_data = new StringBuilder(list_data.substring(0, list_data.length() - 1));
System.out.println("data:" + list_data); // 创建订单-发送请求
StringEntity entity = new StringEntity(list_data.toString(), "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/x-www-form-urlencoded");
httppost.setEntity(entity);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httppost);
String strResult = EntityUtils.toString(response.getEntity());
System.out.println("result:" + strResult + "\t");
httppost.releaseConnection(); return strResult;
} public static void main(String[] args) throws Exception {
create_order("https://test.com", "merchantId",
"key", "pay", "666");
}
}

随机推荐

  1. winform开发 总结1&gt;winform程序使用线程的必要性,以及正确的使用方式

    winform程序中使用线程的必要性: 单线程操作在执行耗时任务时会造成界面假死,带来非常差劲的用户体验,有时候甚至会影响到正常的业务执行,使用多线程做相关操作实属不得已之举. 那么在编写程序之前必须 ...

  2. PHP Warning: date() [function.date]: It is not safe to rely on the system&#39;s timezone

    在用PHP5.3以上的PHP版本时,只要是涉及时间的会报一个 Warning: phpinfo() [function.phpinfo]: It is not safe to rely on the ...

  3. python __file__ 与相对路径

    用__file__ 来获得脚本所在的路径是比较方便的,但这可能得到的是一个相对路径,比如在脚本test.py中写入: #!/usr/bin/env pythonprint __file__ 按相对路径 ...

  4. postgis数据库文件shapefile导入 dbf file (.dbf) can not be opened.shapefile import failed.

    Destination: public.train_polylineSource File: C:\Documents and Settings\Administrator\桌面\ffffff\tra ...

  5. OLAT &amp; OLTP

    联机事务处理(OLTP)和联机分析处理(OLAP)的不同,主要通过以下五点区分开来. 用户和系统的面向性: OLTP是面向顾客的,用于事务和查询处理 OLAP是面向市场的,用于数据分析 数据内容: O ...

  6. 从模态视图push到另一个视图

    //需要给模态视图创建一个Nav,然后再调用presentViewController if (_loginVC == nil) { _loginVC = [[LoginViewController ...

  7. pl/sql 中F8执行单行sql

    pl/sql中设置: tools->preferences->sql window->AutoSelect statement

  8. C#编程经验-VS Debug

    F11 OneStepDebugF10 ProcessDebugbreakPointDebug(quick location,then use one step debug)

  9. Android全局可调试(ro.debuggable = 1)的一种另类改法

    网上流传比较多的,是重打包boot.img.读aosp的init进程源码,发现通过patch init进程也可以实现相同目的. 首先看一下init进程对ro只读属性的检查: /* property_s ...

  10. opencv的基本数据类型CvPoint,CvSize,CvRect,CvScalar

    转自http://blog.csdn.net/gdut2015go/article/details/46301821 opencv的基本数据类型CvPoint,CvSize,CvRect,CvScal ...