Https请求使用curl但使用HttpsURLConnection失败

时间:2016-04-09 14:06:47

标签: java curl httpsurlconnection

我有一个卷曲请求,如下所示: -

curl -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d '{"Request": {"Orders":[{"id_sales_order": 160407400833822,"address_billing": {"first_name":"John","last_name": "Doe","phone": "1234567","phone2": "1234","address1": "Sesamestreet 123","city": "Berlin","postcode": "12345","country": "Germany"}}]}}' "https://debraj:debrajmanna@example.com/oms-api/?Action=UpdateOrderInformation&ServiceName=OMS&Signature=e436d6c7c930fa37a30a8b67051cb4531dad92b0c904a5a009bb4529a762dcd7&Timestamp=2016-04-09T19%3A14%3A12%2B0530&Version=1.0"

这是输出: -

{"ErrorResponse":{"Head":{"RequestAction":"UpdateOrderInformation","ErrorType":"Sender","ErrorCode":0,"ErrorMessage":"Some elements were not processed"},"Body":{"UpdateOrderInformation":[{"ErrorCode":null,"ErrorMessage":"Order with source id: 160407400833822 was not found","Position":0}]}}}

要在Java中实现这一点,我有以下java代码。但是java代码没有按预期运行。有人能让我知道我做错了吗?

public class CurlMain {

    public static void main(String[] args) throws Exception {
    String url = "https://example.com/oms-api/";
    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    con.setRequestMethod("POST");
    String authStr = String.format("%s:%s", "debraj", "debrajmanna");
    String body = "{'Request': {'Orders':[{'id_sales_order': 160407400833822,'address_billing': {'first_name':'John','last_name': 'Doe','phone': '1234567','phone2': '1234','address1': 'Sesamestreet 123','city': 'Berlin','postcode': '12345','country': 'Germany'}}]}}";
    String encodedAuthStr = Base64.getEncoder().encodeToString(authStr.getBytes(StandardCharsets.UTF_8));
    con.setRequestProperty("Authorization", "Basic " + encodedAuthStr);
    con.setRequestProperty("Content-Type", "application/json");
    con.setRequestProperty("Accept", "application/json");

    String urlParameters = "Action=UpdateOrderInformation&ServiceName=OMS&Signature=e436d6c7c930fa37a30a8b67051cb4531dad92b0c904a5a009bb4529a762dcd7&Timestamp=2016-04-09T19%3A14%3A12%2B0530&Version=1.0";

    // Send post request
    con.setDoOutput(true);
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(urlParameters);
    wr.writeBytes(body);
    wr.flush();
    wr.close();

    int responseCode = con.getResponseCode();
    HttpsURLConnection.setFollowRedirects(true);
    Map<String, List<String>> headers = con.getHeaderFields();
    System.out.println(headers.toString());

    BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    System.out.println("Curl Response " + response.toString());
    }

而java代码正在打印: -

{"ErrorResponse":{"Head":{"RequestAction":"","ErrorType":"Sender","ErrorCode":"9","ErrorMessage":"E009: Access Denied"},"Body":""}}

我无法访问服务器代码或服务器配置。我的目标是通过java代码发送相同的curl请求并获得相同的响应。

我正在使用java 8.

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

将查询参数附加到网址解决了以下问题: -

URL obj = new URL(new StringBuilder(url).append("?").append(urlParameters).toString());
...
//wr.writeBytes(urlParameters);