HTTP POST请求失败

时间:2018-05-28 19:54:10

标签: java http curl

我正在与Adyen Payments合作。我在通过3D Secure时对Adyen进行HTTP POST。 CURL是:

curl 'https://test.adyen.com/hpp/3d/validate.shtml'
    -H 'Content-Type: application/x-www-form-urlencoded'
    --data 'PaReq=eNpVUttygjAQ%2FRXrBxAIRFJmzQxix%2FqAMi196FOHiVukI6AB6uXrm6DUNk979nJ292wg3SrE%2BSvKTqGAGJsmy3FUbKbjgHOXUZ85vjvhlDN7LCAJX%2FAg4BtVU9SVcCzbokAGqMuV3GZVKyCTh9lyJZhDXQ%2FIDUGJajkXKTZtUpahPHSFQgXk6oYqK1G8hR9h9BBEO9Ssi%2BKzjdYxkD4Esu6qVp2F6%2BumA4BO7cS2bfdNQMjxeLRkX5rrUkvWJRATB3IfLemM1Wi%2BU7ER63l%2BWaVLd5U%2BufHl3Yu%2FcicO%2BzcFYjJgk7UoqO1wm1E%2BcryA0sDjQHo%2FZKUZRCxmyYgxy7b1tlcP7E2j8AoYM5G%2FHtCKK6zkWXA60esMCPC0ryvUGXrJXxvIfezo2egrW60Zo0ZgNvH5oz1I3QcMS6HV0UPznsYAIKaU3I5IbsfW1r9P8AOBB62l&MD=djIhMWpWZmsxZE5LTFNJYmc5M1ByREFXQT09Ida8tar2LvClbYMvLrtnpYmd9QF91lL5tq1ggkf7cT4KB8uV7hNmv%2FAUfzhuny5nQAkEJCcEpHyxtG%2Fz33mwGDJYJw117NxfSMV64R3FIHfYyTLorMXd656CVrnCFQ5fLtxHiCjLSuNouwNqE03E8PXLmzVCMBGJx%2B8IGCkm%2F3rvXZwucOwtM94uFqm7atGsIEmZ%2F4HW05a9m3YkGBpA1p4CR8x59F2AQSrmIQXfxwswpKvVxV%2FOLZoO4CEgMV3gzSu2Bb2AsfR3p1ZCBXUPh8KvEaiBcQ%2FwPq%2FnGypRHFCSOabqlqUwUWyXAQF%2FtU8g0iVaLOvnucrunQ8%3D&TermUrl=http%3A%2F%2Flocalhost%3A8080%2Fv1%2Fgift%2Fpaymentresponse%2F5aaff1fc-bb84-4f84-8530-54cdc273ddb3'
    -i

运行它会返回一个HTML表单,状态代码为200.

现在我想为此编写一个集成测试。我在做:

HttpPost httpPost = new HttpPost( "https://test.adyen.com/hpp/3d/validate.shtml" );
httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("MD", "djIhMWpWZmsxZE5LTFNJYmc5M1ByREFXQT09Ida8tar2LvClbYMvLrtnpYmd9QF91lL5tq1ggkf7cT4KB8uV7hNmv%2FAUfzhuny5nQAkEJCcEpHyxtG%2Fz33mwGDJYJw117NxfSMV64R3FIHfYyTLorMXd656CVrnCFQ5fLtxHiCjLSuNouwNqE03E8PXLmzVCMBGJx%2B8IGCkm%2F3rvXZwucOwtM94uFqm7atGsIEmZ%2F4HW05a9m3YkGBpA1p4CR8x59F2AQSrmIQXfxwswpKvVxV%2FOLZoO4CEgMV3gzSu2Bb2AsfR3p1ZCBXUPh8KvEaiBcQ%2FwPq%2FnGypRHFCSOabqlqUwUWyXAQF%2FtU8g0iVaLOvnucrunQ8%3D&TermUrl=http%3A%2F%2Flocalhost%3A8080%2Fv1%2Fgift%2Fpaymentresponse%2F5aaff1fc-bb84-4f84-8530-54cdc273ddb3"));
params.add(new BasicNameValuePair("PaReq", "eNpVUttygjAQ%2FRXrBxAIRFJmzQxix%2FqAMi196FOHiVukI6AB6uXrm6DUNk979nJ292wg3SrE%2BSvKTqGAGJsmy3FUbKbjgHOXUZ85vjvhlDN7LCAJX%2FAg4BtVU9SVcCzbokAGqMuV3GZVKyCTh9lyJZhDXQ%2FIDUGJajkXKTZtUpahPHSFQgXk6oYqK1G8hR9h9BBEO9Ssi%2BKzjdYxkD4Esu6qVp2F6%2BumA4BO7cS2bfdNQMjxeLRkX5rrUkvWJRATB3IfLemM1Wi%2BU7ER63l%2BWaVLd5U%2BufHl3Yu%2FcicO%2BzcFYjJgk7UoqO1wm1E%2BcryA0sDjQHo%2FZKUZRCxmyYgxy7b1tlcP7E2j8AoYM5G%2FHtCKK6zkWXA60esMCPC0ryvUGXrJXxvIfezo2egrW60Zo0ZgNvH5oz1I3QcMS6HV0UPznsYAIKaU3I5IbsfW1r9P8AOBB62l"));
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

HttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();

但是我一直保持500的状态。我怎样才能从Java复制这个简单的CURL?

回复是:

HttpResponseProxy{HTTP/1.1 500 Internal Server Error [Date: Mon, 28 May 2018 19:38:21 GMT, Server: Apache, Set-Cookie: JSESSIONID=3AD3E57C6A509938B790B6EFB237AA20.test104e; Path=/hpp; Secure; HttpOnly, P3P: policyref="/w3c/policy.xml", CP="NOI DSP COR CURa ADMa DEVa OUR NOR BUS FIN STA", Strict-Transport-Security: max-age=31536000, Content-Security-Policy-Report-Only: default-src 'self' https://*.adyen.com; script-src 'self' 'unsafe-inline' https://*.adyen.com; style-src 'self' 'unsafe-inline' https://*.adyen.com; img-src *; font-src *; object-src 'none'; frame-src 'self' https://*.adyen.com; form-action 'self' https://*.adyen.com; base-uri https://*.adyen.com; report-uri /hpp/csp;, Content-Length: 15699, Connection: close, Content-Type: text/html;charset=UTF-8] ResponseEntityProxy{[Content-Type: text/html;charset=UTF-8,Content-Length: 15699,Chunked: false]}}

修改

我在Mac上并且无法让小提琴手通过单声道工作。尝试过Wireshark但是太低级了。尽管所有解决方案都要在上面添加要运行的代码,但是我还尝试了Charles Web Proxy,但是它没有接收请求。

 System.setProperty("http.proxyHost", "localhost");
 System.setProperty("http.proxyPort", "8888");

修改

我已经通过Wireshark找到了请求,但所有内容都已加密,因此看不到请求正文。

2 个答案:

答案 0 :(得分:1)

这只是一个猜测,但我怀疑你可能遇到了对参数值进行URL编码的问题。 (基于%2F,它似乎已经过URL编码。)

尝试将未编码的值传递给BasicNameValuePair构造函数。

答案 1 :(得分:0)

我的成就如此:

HttpPost httpPost = new HttpPost( "https://test.adyen.com/hpp/3d/validate.shtml" );
httpPost.addHeader(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
String payload = String.format("PaReq=%s&MD=%s&TermUrl=%s", URLEncoder.encode(paReq, "UTF-8"), URLEncoder.encode(md, "UTF-8"), URLEncoder.encode(termsUrl, "UTF-8"));
StringEntity body = new StringEntity(payload,
        ContentType.APPLICATION_FORM_URLENCODED);
httpPost.setEntity(body);
HttpResponse authResponse = httpclient.execute(httpPost);