【问题标题】:soap - HTTP Error 400. The request has an invalid header namesoap - HTTP 错误 400。请求的标头名称无效
【发布时间】:2016-01-04 10:41:31
【问题描述】:

我正在尝试使用以下代码请求 Web 服务

HttpURLConnection conn = null;
String operation = "validateAddress";
String urlStr = "http://.....";
String requestXML = "<soapenv:Envelope "
        + "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "
        + "xmlns:v3=\"http://cio.att.com/commonheader/v3\" "
        + "xmlns:loc=\"http://ovalsgis.ebm.att.com/locationgeocoderservicerequest.xsd\">"
        + "<soapenv:Body>"
        + "<loc:validateGisAddressRequest "
        + "requestFlag=\"F\" "
        + "telcoFlag=\"N\">"
        + "<loc:FieldedAddressInfo>"
        + "    <loc:houseNumber>10</loc:houseNumber>   "
        + "    <loc:streetName>E MURPHY AVE</loc:streetName>  "
        + "    <loc:city>SAPULPA</loc:city>    "
        + "    <loc:state>OK</loc:state>    "
        + "    <loc:country>USA</loc:country>"
        + "</loc:FieldedAddressInfo>"
        + "</loc:validateGisAddressRequest>"
        + "</soapenv:Body>"
        + "</soapenv:Envelope>";

byte[] reqload = requestXML.getBytes();
URL server = new URL(urlStr.trim());
conn = (HttpURLConnection) server.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty("Username", "abcTest");
conn.setRequestProperty("Password", "abcPassword");
conn.setRequestProperty("WSS-Password Type", "PasswordText");
conn.setRequestProperty("Content-Type", "text/xml");
String reqLen = Integer.toString(reqload.length);
conn.setRequestProperty("Content-Length", reqLen);
conn.setRequestProperty("SOAPAction", operation);
OutputStream os = conn.getOutputStream();
os.write(reqload);
os.flush();
logger.info("Received response Code (" + conn.getResponseCode() + ")");

但我得到以下回应:

错误请求 - 无效标头 HTTP 错误 400。请求的标头名称无效

谁能帮我解决这个问题?

【问题讨论】:

  • 端点无法理解您的请求。使用 SoapUI 测试服务。对于实现,您应该使用 SOAP 库 - 在这里解释:stackoverflow.com/questions/15940234/…
  • @fateddy 感谢您的回复。我忘了在帖子中提到它,我已经用 SoapUI 对其进行了测试,并且可以很好地使用它。我正在尝试使用上述 SAAJ 库,但无法通过身份验证详细信息。您能帮我解决一下吗?
  • 能否请您更新您的代码(您在哪里使用 saaj)?请同时发布 wsdl。

标签: java web-services soap


【解决方案1】:

您应该使用javax.xml.soap 发出 SAOP 请求。 请查看documentation

另外,this answer 可能会有所帮助。

【讨论】:

    【解决方案2】:

    @fateddy

    更新代码:

    public class testWS {
    
    public static void main(String args[]) throws Exception{
        HttpURLConnection conn = null;
        String operation = "validateGisAddress";
        String url = "http://.......";
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);
        printSOAPResponse(soapResponse);
        soapConnection.close();
    
    
    }
    private static SOAPMessage createSOAPRequest() throws Exception {
        String requestXML = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:v3=\"http://cio.att.com/commonheader/v3\" xmlns:loc=\"http://ovalsgis.ebm.att.com/locationgeocoderservicerequest.xsd\"><soapenv:Body><loc:validateGisAddressRequest requestFlag=\"F\" telcoFlag=\"N\" username=\"?\" altLocationPropertiesIndicator=\"?\" superScore=\"?\"><!--You have a CHOICE of the next 2 items at this level--><loc:FieldedAddressInfo><!--Optional:-->       <loc:houseNumber>10</loc:houseNumber>            <loc:streetName>E MURPHY AVE</loc:streetName>            <loc:city>SAPULPA</loc:city>            <loc:state>OK</loc:state>            <loc:country>USA</loc:country></loc:FieldedAddressInfo></loc:validateGisAddressRequest></soapenv:Body></soapenv:Envelope>";
        InputStream is = new ByteArrayInputStream(requestXML.getBytes());
        SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(null, is);
        String authorization = new sun.misc.BASE64Encoder().encode(("abcTest:abcPassword").getBytes());
        MimeHeaders hd = soapMessage.getMimeHeaders();
        hd.addHeader("Authorization", "Basic " + authorization);
        soapMessage.saveChanges();
        soapMessage.writeTo(System.out);
    
        return soapMessage;
    }
    
    
    private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Source sourceContent = soapResponse.getSOAPPart().getContent();
        System.out.print("\nResponse SOAP Message = ");
        StreamResult result = new StreamResult(System.out);
        transformer.transform(sourceContent, result);
    }
    

    }

    【讨论】:

    • 您应该编辑您的问题,而不是提供更新作为答案。你能这样做(并删除这个答案)吗?也请添加 wsdl。
    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 2018-09-04
    • 2022-11-11
    • 2019-05-10
    • 2014-12-11
    • 2019-11-30
    • 2014-01-22
    • 1970-01-01
    相关资源
    最近更新 更多