import java.io.IOException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpState;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
public class DoPost {
/**
* @param args
* @throws IOException
* @throws HttpException
*/
public static void main(String[] args) throws HttpException, IOException {
String body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body><theName xmlns=\"urn:DefaultNamespace\">Lotus</theName></soapenv:Body></soapenv:Envelope>";
HttpClient client = new HttpClient();
PostMethod post = new PostMethod("http://127.0.0.1:80/Axel/MyWebService.nsf/MyWebService");
RequestEntity bodyEntity = new StringRequestEntity(body, "utf-8", null);
//post.setFollowRedirects(true);
String proxyHost = "";
int proxyPort =0 ;
String proxyUser = "";
String proxyPwd = "";
String host = "127.0.0.1";
//String host = "";
int port = 80;
String hostUserName = "axel janssen";
String hostPwd = "udontcare";
if (proxyHost.length() > 0) {
DoPost.initProxy(client, proxyHost, proxyPort, proxyUser, proxyPwd);
}
if (host.length() > 0 ) {
DoPost.initHostAuth(client, host, port, hostUserName, hostPwd);
}
post.setRequestEntity(bodyEntity);
post.addRequestHeader("Content-Type", "text/xml; charset=utf-8");
//post.addRequestHeader("Accept", "application/soap+xml, application/dime, multipart/related, text/*");
//post.addRequestHeader("User-Agent", "Axis/1.4");
//post.addRequestHeader("Host", "192.168.0.61:8080");
//post.addRequestHeader("Cache-Control","no-cache");
//post.addRequestHeader("Pragma", "no-cache");
//post.addRequestHeader("SOAPAction", "");
int result = client.executeMethod(post);
System.out.println ("result=" + result);
String ret = post.getResponseBodyAsString();
System.out.println ("ret=" + ret);
}
private static void initProxy(HttpClient client, final String proxyHost, final int proxyPort,
final String proxyUser, final String proxyPwd) {
client.getHostConfiguration().setProxy(proxyHost, proxyPort);
HttpState state = client.getState();
AuthScope authScope = new AuthScope(proxyHost, proxyPort,
AuthScope.ANY_REALM);
state.setProxyCredentials(authScope, new UsernamePasswordCredentials(
proxyUser, proxyPwd));
client.getParams().setAuthenticationPreemptive(true);
}
/**
* Authentification for remote service.
* @param host remote host
* @param port remote port
* @param hostUserName remote user
* @param hostPwd remote password
*/
private static void initHostAuth(HttpClient client, final String host, final int port,
final String hostUserName, final String hostPwd) {
HttpState state = client.getState();
AuthScope authScope = new AuthScope(host, port, AuthScope.ANY_REALM);
state.setCredentials(authScope, new UsernamePasswordCredentials(
hostUserName, hostPwd));
client.getParams().setAuthenticationPreemptive(true);
}
}