I'm trying to send a simple XML through a java application to this SOAP webservice: http://www.webservicex.net/geoipservice.asmx?op=GetGeoIP
My code is currently like this:
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();
}
I'm not using the WSLD generated classes, as you can see I'm trying to send the XML directly. But I can't seem to get the right response this way, it only returns 302 or 400.
I'm kind of a beginner with using SOAP services, and I don't really know if I'm doing all of this the right way.
Can anyone help me on this?
UPDATE
When I try to make the request through 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>
I get: HTTP Error 400. The request has an invalid header name
Firstly change the url from " http://www.webservicex.net " to " http://www.webservicex.net/geoipservice.asmx ".
Secondly, adding the string as string entity solves the problem.
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);
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.