【问题标题】:Soap api insert data request does not work in JavaSoap api插入数据请求在Java中不起作用
【发布时间】:2020-10-26 13:56:07
【问题描述】:

我必须为一个小项目使用 SOAP api。我现在可以检索数据,但是当我尝试插入数据时,它似乎不起作用并给我这个错误:服务器无法处理请求。 ---> 对象引用未设置为对象的实例。

我正在为 post-request 使用以下 soap 资源:http://webapp.tcscourier.com/codapi/Service1.asmx?op=InsertData

我有以下代码:

 OkHttpClient client = new OkHttpClient();
    MediaType mediaType = MediaType.parse("text/xml");
    RequestBody body = RequestBody.create(mediaType, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\r\n  <soap12:Body>\r\n    <InsertData xmlns=\"http://202.61.51.93:6265/\">\r\n      <userName>string</userName>\r\n      <password>string</password>\r\n      <costCenterCode>string</costCenterCode>\r\n      <consigneeName>string</consigneeName>\r\n      <consigneeAddress>string</consigneeAddress>\r\n      <consigneeMobNo>string</consigneeMobNo>\r\n      <consigneeEmail>string</consigneeEmail>\r\n      <originCityName>string</originCityName>\r\n      <destinationCityName>string</destinationCityName>\r\n      <pieces>string</pieces>\r\n      <weight>string</weight>\r\n      <codAmount>0.2</codAmount>\r\n      <custRefNo>string</custRefNo>\r\n      <productDetails>string</productDetails>\r\n      <fragile>string</fragile>\r\n      <services>string</services>\r\n      <remarks>string</remarks>\r\n      <insuranceValue>string</insuranceValue>\r\n    </InsertData>\r\n  </soap12:Body>\r\n</soap12:Envelope>");
    Request request = new Request.Builder()
            .url("http://webapp.tcscourier.com/codapi/Service1.asmx?op=InsertData")
            .method("POST", body)
            .addHeader("Content-Type", "text/xml")
            .build();
    try {
        Response response = client.newCall(request).execute();

        JSONObject xmlJSONObj = XML.toJSONObject(response.body().string());
        System.out.println(xmlJSONObj.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }

我错过了什么?

【问题讨论】:

    标签: java xml api soap


    【解决方案1】:

    请尝试在请求中添加 SoapAction Header。

    addHeader("soapaction", "http://202.61.51.93:6265/InsertData")

    【讨论】:

    • 一定有某种我们不知道就无法知道的内部需求。我已经尝试过使用另一个 Web 服务,它确实有效。我建议任何想了解 SOAP 的人先研究一下 Postman,然后再转向 Java。对我来说很有价值的资源是以下链接:youtube.com/watch?v=xC4QQCQDDDk
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多