1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
package pgm2;
import java.net.*;
import java.io.*;
public class SendSoap {
public static void main(String[] args) throws Exception {
String soapMessage = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope/\" xmlns:web=\"http://www.webservicex.net/\">"
+ " <soap:Header/>"
+ " <soap:Body>"
+ " <web:GetGeoIP>"
+ "<!--Optional:-->"
+ " <web:IPAddress>8.8.8.8</web:IPAddress>"
+ " </web:GetGeoIP>"
+ "</soap:Body>" + "</soap:Envelope>";
String res = sendSOAP("http://www.webservicex.net/geoipservice.asmx", soapMessage);
System.out.println(res);
}
public static String sendSOAP(String SOAPUrl, String soapMessage)
throws Exception {
URL url = new URL(SOAPUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
byte[] byteArray = soapMessage.getBytes("UTF-8");
httpConn.setRequestProperty("Content-Length", String.valueOf(byteArray.length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction", "");
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
OutputStream out = httpConn.getOutputStream();
out.write(byteArray);
out.close();
BufferedReader in = null;
StringBuffer resultMessage= new StringBuffer();
try {
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null) {
resultMessage.append(inputLine);
}
} finally {
if (in != null) {
in.close();
}
}
return resultMessage.toString();
}
} |
Partager