简体   繁体   中英

400 Bad request when pass xml to webservice

I'm new in webservice.

I've to pass xml to aspx web service called plog.asmx

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; 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

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.

I used this JAX-WS

400 Bad Request sometimes happens when you mismatch the protocol(SOAP or HTTP)

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

I already had "Bad Request" consuming a webservice. 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. 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.

That was our expirence. I hope could helps a little.

I had same issue with HttpURLConnection . Adding the below two properties resolved my 400 Bad Request issue:

  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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