【问题标题】:400 Bad request when pass xml to webservice将 xml 传递给 Web 服务时出现 400 错误请求
【发布时间】:2012-12-01 04:44:06
【问题描述】:

我是网络服务的新手。

我必须将 xml 传递给名为 plog.asmx 的 aspx Web 服务

这是我的代码

String xmldata = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
            "<SOAP:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " + 
              "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + 
              "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" >" +
              "<![CD[<soap:Body>" +
              "<SubmitJob xmlns=\"http://www.xdel.biz/XWS/\"> " +
              "<APIKey>"+ API_KEY +"</APIKey>" +
              "<Job>" +
               "<Customer_Name>"+ Customer_Name +"</Customer_Name>" +
               "<Address1>"+ Address1 +"</Address1>" +
                "<Address2>"+ Address2 +"</Address2>" +
                "<Postal_Code>"+ Postal_Code +"</Postal_Code>" +
                "<Phone_Number>"+ Phone_Number +"</Phone_Number>" +
                "<Mobile_Number>"+ Mobile_Number +"</Mobile_Number>" +
                "<Order_Reference>"+ Order_Reference +"</Order_Reference>" +
                "<Delivery_Instructions>"+ Delivery_Instructions +"</Delivery_Instructions>" +
              "</Job>]]>" +
            "</SubmitJob>" +
              "</soap:Body>]]>" +
              "</SOAP:Envelope>";

             System.out.println(xmldata); 


              try{
                  //Create socket
                  String hostname = "www.xdel.biz";
                  int port = 80;
                  InetAddress  addr = InetAddress.getByName(hostname);                    
                  Socket sock = new Socket(addr, port);
                  System.out.println(sock.toString());                    

                  //Send header
                  String path = "/xws/plog.asmx";
                  BufferedWriter  wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));
                  // You can use "UTF8" for compatibility with the Microsoft virtual machine.
                  wr.write("POST " + path + " HTTP/1.1\r\n");
                  wr.write("Host: www.xdel.biz\r\n");
                  wr.write("Content-Type: text/xml; charset=utf-8\r\n");
                  wr.write("Content-Length: " + xmldata.length() + "\r\n");                   
                  wr.write("SOAPAction: \"http://www.xdel.biz/XWS/SubmitJob\" \r\n");
                  wr.write("\r\n");

                  //Send data
                  wr.write(xmldata);
                  wr.flush();

                  System.out.println("1");

                  // Response
                  BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                  String line;
                  while((line = rd.readLine()) != null){
                      System.out.println(line);
                  }

                } catch (Exception e) {
                  e.printStackTrace();
                }

当我运行代码时,我得到了这样的错误

HTTP/1.1 400 错误请求 缓存控制:私有 内容类型:文本/xml;字符集=utf-8 服务器:Microsoft-IIS/7.5 X-AspNet-版本:4.0.30319 X-Powered-By: ASP.NET 日期:2012 年 12 月 13 日星期四 09:37:12 GMT 内容长度:0

我在谷歌上搜索了错误并尝试修复,但没有找到解决方案..

【问题讨论】:

    标签: java web-services


    【解决方案1】:

    一个好主意是使用实现 SOAP Web 服务并且已经过测试的 API。

    我用过这个 JAX-WS

    当您不匹配协议(SOAP 或 HTTP)时,有时会发生 400 Bad Request

    【讨论】:

    【解决方案2】:

    可能是&lt;![CD[&lt;soap:Body&gt;&lt;/soap:Body&gt;]]&gt; 尝试使用without ![CD[ ]] block

    【讨论】:

      【解决方案3】:

      我已经有“错误请求”在使用网络服务。问题是,在寻找答案将近一天之后,我们发现这是消耗的 XML 的大小,消耗的 SOAP 消息的大小。问题是,提供要使用的 Web 服务的应用程序必须设置为接收大型 XML 数据,我们必须将应用程序服务器配置为扩展以增加用于从客户端接收 SOAP 消息的缓冲区大小.

      那是我们的到期日。我希望能帮上一点忙。

      【讨论】:

        【解决方案4】:

        HttpURLConnection 有同样的问题。添加以下两个属性解决了我的 400 Bad Request 问题:

        1. httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
        2. httpConn.setRequestProperty("soapAction", soapAction);

        注意:当您尝试读取响应时,通常会出现此错误。

        【讨论】:

        • 我需要帮助,遇到这个问题。你能告诉我你是如何发送 SOAP 信封的吗
        猜你喜欢
        • 1970-01-01
        • 2016-06-14
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-24
        相关资源
        最近更新 更多