如何使用 header、body 和参数发出 XML api 请求?

How to make XML api request with header, body and params?

我正在使用 https://gist.github.com/itsalif/6149365 库来发出 XML 请求。 它使用 Simple-XML 将 XML 序列化为 Objects。

当没有 header 并且参数与 SOAP api 关联时,我能够成功地发出 XML 请求。但是,在执行由 header 和参数组成的小复杂请求时,我无法得到响应。

我的请求格式是这样的

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.holidaywebservice.com/HolidayService_v2/">
 <SOAP-ENV:Body>
   <ns1:GetHolidaysAvailable>
     <ns1:countryCode>UnitedStates</ns1:countryCode>
   </ns1:GetHolidaysAvailable>
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

代码

HashMap<String, String> mapData = new HashMap<>();
final String mRequestBody = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://www.holidaywebservice.com/HolidayService_v2/\">\n" +
        " <SOAP-ENV:Body>\n" +
        "   <ns1:GetHolidaysAvailable>\n" +
        "     <ns1:countryCode>UnitedStates</ns1:countryCode>\n" +
        "   </ns1:GetHolidaysAvailable>\n" +
        " </SOAP-ENV:Body>\n" +
        "</SOAP-ENV:Envelope>";

SimpleXmlRequest dellaRequest = new SimpleXmlRequest<String>
        (Request.Method.POST, "http://www.holidaywebservice.com//HolidayService_v2/HolidayService2.asmx?wsdl", String.class, headerDataMap, bodyDataMap,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Log.d("BaseActivity", "response = " + response.toString());

                    }
                },
                new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {

                        Log.d("BaseActivity", "Error:" + error.getMessage());

                    }
                }
        ){
    @Override
    public byte[] getBody() throws AuthFailureError {

        try {
            return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                    mRequestBody, "utf-8");
            return null;
        }
    }
};

我收到 400 响应代码,即 BAD REQUEST,声明提供了无效输入,例如验证错误或丢失数据。

这个 api 在 Postman rest 客户端中工作正常。所以我无法弄清楚我做错了什么。

您可以使用 Volley 有效地调用此 XML 请求。

String url = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx";


//Volley request
StringRequest request = new StringRequest(Request.Method.POST, url,

        new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

                Log.d("Response", response);

            }
        },
        new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Log.d("Error", error.getMessage());

            }
        }) {

    @Override
    public String getBodyContentType() {
        // set body content type
        return "text/xml; charset=UTF-8";
    }

    @Override
    public byte[] getBody() throws AuthFailureError {

        try {
            return reqXML.getBytes("UTF-8");
        } catch (UnsupportedEncodingException uee) {
            // TODO consider if some other action should be taken
            return null;
        }
    }

};
//
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);

//Adding request to the queue
requestQueue.add(request);