简体   繁体   English

具有HttpURLConnection的Java cURL替代

[英]Java cURL alternative with HttpURLConnection

I was asked to port a PHP module I was writing to Java. 我被要求移植我正在编写的Java PHP模块。 I was previously using PHP's native cURL library, now trying to achieve the same action with HttpURLConnection. 我以前使用的是PHP的本机cURL库,现在尝试使用HttpURLConnection实现相同的操作。

Here's the call I want to do with cURL: 这是我要使用cURL进行的呼叫:

curl -u 'ExactID:Password' \
     -H 'Content-Type: application/json; charset=UTF-8' \
     -H 'Accept: application/json' \
     -d '{
 "transaction_type":"00",
 "amount":"15.75",
 "cardholder_name":"PaulTest",
 "transarmor_token":"3000",
 "credit_card_type":"Visa",
 "cc_expiry":"0016",
}' \
https://api.demo.globalgatewaye4.firstdata.com/transaction/v11

Here's what I have in Java, which returns a HTTP 400 error. 这是我在Java中所拥有的东西,它返回HTTP 400错误。 Any ideas? 有任何想法吗?

public static void main(String[] args) {     
    URL url = new URL("https://api.demo.globalgatewaye4.firstdata.com/transaction/v11");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    conn.setRequestProperty("Accept", "application/json");

    String userpass = "ExactID" + ":" + "Password";
    String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes()));
    conn.setRequestProperty ("Authorization", basicAuth);

    JSONObject obj = new JSONObject();
    obj.put("transaction_type", "00");
    obj.put("amount", "10");
    obj.put("cardholder_name", "PaulTest");
    obj.put("transarmor_token", "3000");
    obj.put("cc_expiry", "0016");
    obj.put("credit_card_type", "Visa");

    String input = obj.toString();
    System.out.println(input);

    OutputStream os = conn.getOutputStream();
    os.write(input.getBytes());
    os.flush();

    if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {

    throw new RuntimeException("Failed : HTTP error code : "+ conn.getResponseCode() + conn.getResponseMessage());

}

One ambiguity in your java code is on string to byte array encoding. Java代码中的一个歧义是字符串到字节数组编码。 By default java will use your default platform encoding, but it's a good practice to express it explicitly because it often lead to hard to track bug 默认情况下,java将使用您的默认平台编码,但这是一种明确表达的好习惯,因为它通常会导致难以跟踪的错误

String basicAuth = "Basic " + new String(new Base64().encode(userpass.getBytes("ISO-8859-1")));

To be sure also check the encoded base 64 value generated by java on curl by using 还要确保使用以下命令检查curl上java生成的编码基数64值:

-H 'Authorization: Basic ....`

Instead of -u 代替-u

Also I'd try to cast the created URLConnection to HttpsURLConnection. 另外,我尝试将创建的URLConnection强制转换为HttpsURLConnection。 Thay may/not make difference Thay可能/没有影响

HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

After tinkering around, I made two mistakes: 修补之后,我犯了两个错误:

For this POST method, basic authentication was not required. 对于此POST方法,不需要基本身份验证。 The user & pw goes into the JSON body along with the other parameters. 用户&pw与其他参数一起进入JSON正文。

Also, my "transarmor_token" field needed to be 16 digits. 另外,我的“ transarmor_token”字段必须为16位数字。

Conclusion: HttpURLConnection is a great cURL alternative. 结论:HttpURLConnection是一个很好的cURL替代方案。 Forget about using the curl-java binding. 忘记使用curl-java绑定了。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM