test program:
import java.net.*;
import java.io.*;
public class Test {
public static void main(String[] args) {
String input = "<elements>\n" +
" <element>1</element>\n" +
" <element>2</element>\n" +
" <element>3</element>\n" +
"</elements>\n";
String output = execute(input);
System.out.println("Input:\n" + input);
System.out.println("Output:\n" + output);
}
private static String execute(String input) {
StringBuffer sb = new StringBuffer();
try {
URL url = new URL("
http://localhost:8080/test/VectorAddServlet");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.getOutputStream().write(input.getBytes());
con.connect();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = con.getInputStream();
byte[] b = new byte[1000];
int n;
while ((n = is.read(b)) >= 0) {
sb.append(new String(b, 0, n));
}
is.close();
}
con.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}