Posts

Showing posts from February, 2013

Send POST request using java.net.URLConnection and parse xml response

Send POST request on server....
-------------------------------------------------------------------

public static StringBuffer getWebServiceResponse(String urlString,
String to, String from, String subject, String mailBody) {
StringBuffer dataBuffer = new StringBuffer();
String charset = "UTF-8";

String query;
OutputStream output = null;
try {
query = String.format(
"to=%s&from=%s&subject=%s&mailBody=%s",
URLEncoder.encode(to, charset),
URLEncoder.encode(from, charset),
URLEncoder.encode(subject, charset),
URLEncoder.encode(mailBody, charset));
            log.info("query:"+query);
URLConnection connection;
if (ETradeConstants.PROXY_URL != null) {
connection = new URL("http", "10.10.10.1", 3128, urlString)
.openConnection(); // For MA local machine
} else {
connection = new URL(urlString).openConnection(); // For AppEngine
}

connection.setDoOutput(true); // Triggers POST.
connection.setRequestProperty("Accept-Charset", charset);
connect…