Urgent! I need help on a Java project for school! Can you fix this code?
Urgent! I need help on a Java project for school! Can you fix this code?
I am working on a project for my AP Java (Computer Science A) class and I need your help! I am trying to post data to a html form from a particular website:
http://www.upcdatabase.com/itemform.asp
The Java code I wrote kind of sort of works, but returns the website source code that doesn’t contain the results of the query!
If I manually type in the following UPC code to the form: 075992584425
It will return: Description: Madonna – Like A Prayer
Size/Weight: CD … and a bunch of other corresponding data.
The Java Class code that I wrote for this:
// BEGIN CODE HERE ——————————————————————————–
import java.net.*;
import java.io.*;
public class ReadWriteURL {
public void PostToForm() {
try
{
//Create Post String
String data = URLEncoder.encode(“075992584425″, “UTF-8″);
System.out.println(“data= ” + data);
System.out.println();
// Send Data To Page
URL url = new URL(“http://www.upcdatabase.com/itemform.asp?”);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new
OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get The Response
BufferedReader rd = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
System.out.println(line);
//you Can Break The String Down Here
}
}
catch (Exception e) {
System.out.println(” There was an error”);
}
}
}
// END CODE ———————————————————————————————-
Can you quickly fix this code so that it retrieves the correct data, for the search value? (Same data like entering by hand)
I cannot figure out from the source code of the HTML page what to send or how to find out what the exact URL is to send it to.
Tags: code, HELP, java, need, Project, school, this, Urgent
Under Forum

1 Comment for Urgent! I need help on a Java project for school! Can you fix this code?
1. om | February 15th, 2011 at 8:24 am
That itemform.asp page uses JavaScript to catch the form submission and construct an URL that refers to the data for the UPC code you entered into the form.
That URL is “http://www.upcdatabase.com/item/” followed by the UPC code. So, for instance, the URL for your example would be “http://www.upcdatabase.com/item/075992584425″. You should see that URL in your browser’s address bar when the answer page is shown.
If you make your URLConnection to that URL instead of to the itemform.asp page then you don’t need to worry about the form and you don’t need to send any input into the connection. You should be able to simply read the result.
Leave a Comment for Urgent! I need help on a Java project for school! Can you fix this code?
You must be logged in to post a comment.
Trackback this post | Subscribe to the comments via RSS Feed