简体   繁体   English

将XML传递到Web服务时出现400错误的请求

[英]400 Bad request when pass xml to webservice

I'm new in webservice. 我是Web服务的新手。

I've to pass xml to aspx web service called plog.asmx 我必须将xml传递给名为plog.asmx的aspx Web服务

here is my code 这是我的代码

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();
                }

when I run the code, I got error like this 当我运行代码时,出现这样的错误

HTTP/1.1 400 Bad Request Cache-Control: private Content-Type: text/xml; HTTP / 1.1 400错误的请求缓存控制:私有Content-Type:text / xml; charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Thu, 13 Dec 2012 09:37:12 GMT Content-Length: 0 charset = utf-8服务器:Microsoft-IIS / 7.5 X-AspNet-版本:4.0.30319 X-Powered-通过:ASP.NET日期:2012年12月13日,星期四09:37:12 GMT内容长度:0

I googled the error and tried to fix but no solution come out.. 我搜索了该错误,并尝试修复,但没有解决方案。

A good ideia would be to use an API that implements SOAP webservice and is already tested. 一个好的想法是使用实​​现SOAP Web服务并且已经过测试的API。

I used this JAX-WS 我用这个JAX-WS

400 Bad Request sometimes happens when you mismatch the protocol(SOAP or HTTP) 当协议(SOAP或HTTP)不匹配时,有时会发生400错误请求

可能是<![CD[<soap:Body></soap:Body>]]>尝试在without ![CD[ ]] block

I already had "Bad Request" consuming a webservice. 我已经有消耗Web服务的“错误请求”。 The thing is, after almost a day looking for an answer, we found out that was the size of the XML consumed, the size of the SOAP Message consumed. 问题是,经过将近一天的寻找答案之后,我们发现这是消耗的XML的大小,消耗的SOAP消息的大小。 The problem is, the application that provides the Webservice to be consumed, must be set up to receive a large XML Data, we had to config our application server to expand to encrease the size of our buffer used to receive the SOAP Message from the client. 问题是,必须将提供要使用的Web服务的应用程序设置为接收大的XML数据,我们必须配置应用程序服务器以进行扩展以增加用于从客户端接收SOAP消息的缓冲区的大小。

That was our expirence. 那是我们的到期日。 I hope could helps a little. 我希望可以有所帮助。

I had same issue with HttpURLConnection . 我在HttpURLConnection遇到了同样的问题。 Adding the below two properties resolved my 400 Bad Request issue: 添加以下两个属性解决了我的400 Bad Request问题:

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

Note: this error usually appears when you try to read response. 注意:当您尝试读取响应时,通常会出现此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM