Unable to write/post to a URL from Java
Hi,
I need to write 10101663 to the text field at treasurer.maricopa.gov/Parcel/Summary.aspx , hit Enter or Submit, then read the same URL after a few seconds.
The problem is, I need to do it from Java code.
I tried the code below, but it's not working. The data I read from treasurer.maricopa.gov/Parcel/Summary.aspx after trying to write 10101663 is the same data that was there all along, as if I didn't write anything.
Below is one of the variants of several pieces of code that I tried, all unsuccessful. Could you tell me what's missing from my code ?
import java.io.*;
import java.util.*;
import java.net.*;
public class WebWriteRead {
public static void main(String args[]) {
try {
URL taxIDURL = new URL("http://treasurer.maricopa.gov/Parcel/Summary.aspx");
HttpURLConnection con = (HttpURLConnection) taxIDURL.openConnection();
con.setDoInput (true);
con.setDoOutput (true);
con.setUseCaches (false);
con.setRequestMethod("POST");
con.setFollowRedirects(true);
String msg = URLEncoder.encode("10101663");
con.setRequestProperty("Content-Length", "" + msg.length());
con.setRequestProperty("Content-Language", "en-US");
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
DataOutputStream os = new DataOutputStream(con.getOutputStream());
// OutputStreamWriter osw = new OutputStreamWriter(os);
os.writeBytes(msg);
os.flush();
os.close();
// any response?
InputStreamReader isr = new InputStreamReader(con.getInputStream());
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
System.out.println("line: " + line);
}
catch (MalformedURLException e) {
System.out.println("Malformed URL: http://treasurer.maricopa.gov/parcels/default.asp?Parcel=10101663");
}
catch (IOException e) {
System.out.println("scanTreasurersWebSite - Caugh Exception " + e.toString());
}
}
}