Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

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?%22);

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.

2 Answers

Relevance
  • om
    Lv 6
    1 decade ago
    Favorite Answer

    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/%22 followed by the UPC code. So, for instance, the URL for your example would be "http://www.upcdatabase.com/item/075992584425%22. 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.

  • bohrn
    Lv 4
    5 years ago

    i assume you opt to enhance a JAVA IDE... somebody already listed out that lot of loose IDE's available and he's very suitable - they are incredibly lots complicated to get a draw close escially in case you're on a good timeline. i can recommend you that attempt finding out an simple text fabric editor on the internet first. So atleast you do no longer would desire to waste time on coming up text fabric processing code like reproduction/paste, etc. next element could be to characteristic yet another decision to the text fabric editor for compiling the code. least perplexing option is to make a call to the OS and get in touch with the javac alongside with the record call and placement. next attempt giving some concepts with the carry at the same time command. this way you could incrementally upload greater function. as quickly as you get the text fabric editor to hold at the same time your code, atleast you could ' in theory' call it a Java IDE.

Still have questions? Get your answers by asking now.