java - 将POST xml发送到SOAP Web服务不起作用

时间:2018-02-18 15:58:38

标签: java xml web-services soap

我试图通过Java应用程序将简单的XML发送到此SOAP Web服务: http://www.webservicex.net/geoipservice.asmx?op=GetGeoIP

我的代码目前是这样的:

        String url = "http://www.webservicex.net"; 
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);

        post.setHeader("Host", "www.webservicex.net");
        post.setHeader("Content-Type", "text/xml;charset=utf-8");
        post.setHeader("SOAPAction", "http://www.webservicex.net/GetGeoIP");

        String xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + 
                "<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/\">\r\n" + 
                "  <soap:Body>\r\n" + 
                "    <GetGeoIP xmlns=\"http://www.webservicex.net/\">\r\n" + 
                "      <IPAddress>50.207.31.216</IPAddress>\r\n" + 
                "    </GetGeoIP>\r\n" + 
                "  </soap:Body>\r\n" + 
                "</soap:Envelope>";

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("xml", xmlString));

        post.setEntity(new UrlEncodedFormEntity(urlParameters));
        HttpResponse response;
        try {
            response = client.execute(post);
            System.out.println("Response Code : " + 
                                            response.getStatusLine().getStatusCode());

            BufferedReader rd = new BufferedReader(
                                new InputStreamReader(response.getEntity().getContent()));

            StringBuffer result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            System.out.println(result.toString());

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

我没有使用WSLD生成的类,因为您可以看到我尝试直接发送XML。但我似乎无法以这种方式得到正确的答案,它只返回302或400。

我是一名使用SOAP服务的初学者,我真的不知道我是否正确地做了这一切。

有人可以帮我吗?

更新

当我尝试通过Advanced Rest Client发出请求时:

Host: www.webservicex.net
Content-Type: application/xml
Content-Length: 362
SOAPAction: http://www.webservicex.net/GetGeoIP
<?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/">
  <soap:Body>
    <GetGeoIP xmlns="http://www.webservicex.net/">
      <IPAddress>string</IPAddress>
    </GetGeoIP>
  </soap:Body>
</soap:Envelope>

我得到: HTTP错误400.请求的标题名称无效

1 个答案:

答案 0 :(得分:3)

首先从&#34; http://www.webservicex.net&#34;更改网址到&#34; http://www.webservicex.net/geoipservice.asmx&#34;。

其次,将字符串添加为字符串实体可以解决问题。

 StringEntity  xmlString = new StringEntity( "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" +
                "<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/\">\r\n" +
                "  <soap:Body>\r\n" +
                "    <GetGeoIP xmlns=\"http://www.webservicex.net/\">\r\n" +
                "      <IPAddress>50.207.31.216</IPAddress>\r\n" +
                "    </GetGeoIP>\r\n" +
                "  </soap:Body>\r\n" +
                "</soap:Envelope>");
        post.setEntity(xmlString);