将发布XML发送到SOAP Web服务不起作用



我正在尝试通过Java应用程序将简单的XML发送到此SOAP WebService: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"?>rn" + 
                "<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/">rn" + 
                "  <soap:Body>rn" + 
                "    <GetGeoIP xmlns="http://www.webservicex.net/">rn" + 
                "      <IPAddress>50.207.31.216</IPAddress>rn" + 
                "    </GetGeoIP>rn" + 
                "  </soap:Body>rn" + 
                "</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服务的初学者,我真的不知道我是否以正确的方式进行所有操作。

有人可以帮助我吗?


update

当我尝试通过高级REST客户端提出请求时:

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。请求有一个无效的标题名称

首先将URL从" http://www.webservicex.net"更改为" http://www.webservicex.net/geoipservice.asmx"。

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

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

相关内容

  • 没有找到相关文章

最新更新