I want to upload a file (GPX file, which is similar to XML) to a URL (http://www.gpsvisualizer.com/map_input). Then wait for the map to be generated and retrieve it. The developer has stated this is possible, but I am not getting anywhere. Here is my code so far:
private static void postData(File fileName) {
File input = fileName;
//Setup connection
URL url = new URL("http://www.gpsvisualizer.com/map");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/xml");
connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)");
//Send file
OutputStream os = connection.getOutputStream();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
FileReader fileReader = new FileReader(input);
StreamSource source = new StreamSource(fileReader);
StreamResult result = new StreamResult(os);
transformer.transform(source, result);
os.flush();
//Retrieve response
BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String line = br.readLine();
while ( line != null ) {
System.out.println(line);
line = br.readLine();
}
br.close();
}
All this is doing, is setting up the connection, when I get the response, I am only getting the HTML of the initial page, not the (should be) map one.
connection
will contain everything in the body of the HTTP response. You have to parse it and extract what you want. – Sotirios Delimanolis Mar 27 at 16:09Draw the map
button, your browser attaches the different input parameters included in the enclosing form to your Http Post request. If you're going to send the same request through java code, you need to put those parameters in yourself. Use a tool like Firebug to find out which parameters are needed and write those into your request. Look into multipart requests since you are doing a file upload. – Sotirios Delimanolis Mar 27 at 16:23