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
   |  
import java.net.*;
import java.io.*;
 
public class SendSoap {
	public static void main(String[] args) throws Exception {
		String soapMessage = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:oca=\"http://oca\">"
				+ " <soap:Header/>"
				+ "  <soap:Body>"
				+ " <oca:sayHello>"
				+ "  <name>Olivier</name>"
				+ " </oca:sayHello>"
				+ "</soap:Body>" + "</soap:Envelope>";
 
		String res = sendSOAP("http://localhost:6060/axis2/services/Service1", 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