将xml传递给webservice时出现400错误请求

时间:2012-12-13 10:03:39

标签: java web-services

我是网络服务的新手。

我要将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错误请求   缓存控制:私有   Content-Type:text / xml;字符集= utf-8的   服务器:Microsoft-IIS / 7.5   X-AspNet-Version:4.0.30319   X-Powered-By:ASP.NET   日期:2012年12月13日星期四09:37:12 GMT   内容长度:0

我用谷歌搜索错误并尝试修复但没有解决方案出来..

3 个答案:

答案 0 :(得分:0)

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

我用过这个 JAX-WS

400错误请求有时会在您不匹配协议(SOAP或HTTP)时发生

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我与HttpURLConnection有同样的问题。添加以下两个属性解决了我的400错误请求问题:

  1. httpConn.setRequestProperty("Content-Type","text/xml; charset=utf-8");
  2. httpConn.setRequestProperty("soapAction", soapAction);
  3. 注意:当您尝试阅读回复时,通常会出现此错误。