Her er en lettere pyntet udgave:
import java.net.*;
import java.io.*;
public class WSTest {
public String marshall() {
StringBuffer payload = new StringBuffer("<?xml version='1.0' encoding='UTF-8'?>");
payload.append("<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"
+ "<getKeywords xmlns='
http://tempuri.org/'>\r\n" + "<Lang>english</Lang>\r\n"
+ "</getKeywords>\r\n"
+ "</soap:Body>\r\n"
+ "</soap:Envelope>\r\n");
return payload.toString();
}
public void post() {
try {
String request = marshall();
URL url = new URL("
http://authors.aspalliance.com/dotnetsolutions/WebService11/Service1.asmx");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setAllowUserInteraction(false);
con.setRequestProperty("Content-Length", Integer.toString(request.length()));
con.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
con.setRequestProperty("SOAPAction", "\"
http://tempuri.org/getKeywords\"");
OutputStream out = con.getOutputStream();
out.write(request.getBytes());
out.flush();
out.close();
System.out.println(con.getResponseCode() + " - " + con.getResponseMessage());
InputStream in = con.getInputStream();
byte[] b = new byte[1000];
int n;
while((n = in.read(b)) >= 0) {
System.out.println(new String(b,0,n));
}
} catch (MalformedURLException e1) {
System.out.println(e1.getMessage());
} catch (IOException e2) {
System.out.println(e2.getMessage());
}
}
public static void main(String[] args) {
WSTest sc = new WSTest();
sc.post();
}
}