I'm writing a program to post data to a particular URL, retrieve the response and, based on the response, submit another post to the same URL. The first post works fine but I get a FileNotFoundException on the second post. I checked the data and the url and they are correct and the same string pasted into my browser's address bar works correctly.
Here is the relevant code:
//Build Query-string for request.
formData.append"company=&userid=xxxxxxxx&ip=100.100.100.01&auto=n&brush=n&crime=n&ppc=a&wind=n&quake=n"
formData.append("&rnumber=" + URLEncoder.encode(number.trim()));
formData.append("&rpre=" + URLEncoder.encode(prefix.trim()));
formData.append("&rname=" + URLEncoder.encode(address.trim()));
formData.append("&rtype=" + URLEncoder.encode(type.trim()));
formData.append("&rpost=" + URLEncoder.encode(suffix.trim()));
formData.append("&rcity=" + URLEncoder.encode(city.trim()));
formData.append("&rstate=" + URLEncoder.encode(state.trim()));
formData.append("&rzip=" + URLEncoder.encode(zip.trim()));
//Get page content for first pass. println's are for debugging
System.out.println(formData.toString());
System.out.println();
content = getISOpage(formData.toString());
//After parsing content to get the values, I build the 2nd request
//Build form data for second pass
formData = new StringBuffer();
formData.append("long=" + URLEncoder.encode(longitude));
formData.append("&lat=" + URLEncoder.encode(latitude));
formData.append("&radius=" + URLEncoder.encode(radius));
formData.append("&ordernum=" + URLEncoder.encode(orderNum));
formData.append("&addr1=" + URLEncoder.encode(addr1));
formData.append("&addr2=" + URLEncoder.encode(addr2));
System.out.println(formData.toString());
System.out.println();
//Get page content for second pass
content = getISOpage(formData.toString());
And the code for the getISOpage method is:
public static String getISOpage(String formData){
String line;
String url = " String password = "xxxxxxx:yyyyyyy";
//String auth = "Basic " + new sun.misc.BASE64Encoder().encode(password.getBytes());
StringBuffer content = new StringBuffer();
try{
HttpURLConnection urlcon = (HttpURLConnection) new URL(url).openConnection();
urlcon.setRequestProperty("Authorization", auth);
urlcon.setRequestProperty("Content-type", "application/x-
urlcon.setRequestMethod("POST"
urlcon.setDoOutput(true);
urlcon.setDoInput(true);
PrintWriter pout = new PrintWriter(new OutputStreamWriter(urlcon.getOutputStream(), "8859_1", true);
pout.print(formData);
pout.flush();
pout.close();
BufferedReader bin = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
while((line = bin.readLine()) != null){
System.out.println(line);
content.append(line);
}
}
catch(Exception e){
System.out.println();
System.out.println(e);
System.out.println(e.getMessage());
}
return content.toString();
}
As I said, the first request works fine, I parse those results and build the second request on that. Then the second request bombs on me.
This is my first java program and I'm not very comfortable with the language yet so any assistance would be greatly appreciated.
Thanks,
bvallet
Here is the relevant code:
//Build Query-string for request.
formData.append"company=&userid=xxxxxxxx&ip=100.100.100.01&auto=n&brush=n&crime=n&ppc=a&wind=n&quake=n"
formData.append("&rnumber=" + URLEncoder.encode(number.trim()));
formData.append("&rpre=" + URLEncoder.encode(prefix.trim()));
formData.append("&rname=" + URLEncoder.encode(address.trim()));
formData.append("&rtype=" + URLEncoder.encode(type.trim()));
formData.append("&rpost=" + URLEncoder.encode(suffix.trim()));
formData.append("&rcity=" + URLEncoder.encode(city.trim()));
formData.append("&rstate=" + URLEncoder.encode(state.trim()));
formData.append("&rzip=" + URLEncoder.encode(zip.trim()));
//Get page content for first pass. println's are for debugging
System.out.println(formData.toString());
System.out.println();
content = getISOpage(formData.toString());
//After parsing content to get the values, I build the 2nd request
//Build form data for second pass
formData = new StringBuffer();
formData.append("long=" + URLEncoder.encode(longitude));
formData.append("&lat=" + URLEncoder.encode(latitude));
formData.append("&radius=" + URLEncoder.encode(radius));
formData.append("&ordernum=" + URLEncoder.encode(orderNum));
formData.append("&addr1=" + URLEncoder.encode(addr1));
formData.append("&addr2=" + URLEncoder.encode(addr2));
System.out.println(formData.toString());
System.out.println();
//Get page content for second pass
content = getISOpage(formData.toString());
And the code for the getISOpage method is:
public static String getISOpage(String formData){
String line;
String url = " String password = "xxxxxxx:yyyyyyy";
//String auth = "Basic " + new sun.misc.BASE64Encoder().encode(password.getBytes());
StringBuffer content = new StringBuffer();
try{
HttpURLConnection urlcon = (HttpURLConnection) new URL(url).openConnection();
urlcon.setRequestProperty("Authorization", auth);
urlcon.setRequestProperty("Content-type", "application/x-
urlcon.setRequestMethod("POST"
urlcon.setDoOutput(true);
urlcon.setDoInput(true);
PrintWriter pout = new PrintWriter(new OutputStreamWriter(urlcon.getOutputStream(), "8859_1", true);
pout.print(formData);
pout.flush();
pout.close();
BufferedReader bin = new BufferedReader(new InputStreamReader(urlcon.getInputStream()));
while((line = bin.readLine()) != null){
System.out.println(line);
content.append(line);
}
}
catch(Exception e){
System.out.println();
System.out.println(e);
System.out.println(e.getMessage());
}
return content.toString();
}
As I said, the first request works fine, I parse those results and build the second request on that. Then the second request bombs on me.
This is my first java program and I'm not very comfortable with the language yet so any assistance would be greatly appreciated.
Thanks,
bvallet