Results 1 to 2 of 2

Thread: HTML Post

  1. #1
    zoukai957 is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Default HTML Post

    Hi I'm trying to make a program that will fill out a form (involving scroll downs) and click submit. For some reason I keep on getting an IO (file not found error). I think it might be because I'm writing the POST script wrong or I didn't add the click submit button to the script (If that is the issue, the submit button does not have a name field... How can I tell my script to click submit if it has no name field) Can someone please help me? Sorry if this is the wrong section to post this >.<

    Java Code:
    import java.net.*;
    import java.io.*;
    
    public class Code {
    
    	public static void main(String[] args) throws IOException {
    	
    		//Creates the URL where you enter the class
    		try {
    			URL webreg = new URL("http://soc.ess.rutgers.edu/soc");
    		} catch (MalformedURLException e) { //error if the above URL is not accessible
    			// TODO Auto-generated catch block
    			System.out.println("This program cannot access the class list website. Sucks to be you xP");
    			return;
    		}
    		
    		//Writes the class you want to submit
    		String content ="p_subj_cd=" + URLEncoder.encode("750", "UTF-8") + "&" + "p_course_no=" + URLEncoder.encode("204", "UTF-8")
    			+ "&" + "p_campus=" + URLEncoder.encode("NB", "UTF-8") + "&" + "p_yearterm=" + URLEncoder.encode("20111", "UTF-8");
    		
    		//Tells the program that we are writing to the code given to us from the URL, webreg
    		HttpURLConnection webregConnection =null;
    		DataOutputStream output = null; //temporary for now
    		try {
    			webregConnection = (HttpURLConnection) (new URL("http://soc.ess.rutgers.edu/soc").openConnection());
    			//Specifies that we are gonna input something into this connection
    			webregConnection.setDoInput(true);
    			//Specifies that we are gonna use this connection for an output
    			webregConnection.setDoOutput(true);
    			
    			
    			/*possible error below for wrong content type
    			 * 
    			 * 
    			sets the content type of the website */
    			webregConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    			
    			//States the PHP command Post
    			webregConnection.setRequestMethod("POST");
    			
    			//gets the output after submitting the class
    			output = new DataOutputStream(webregConnection.getOutputStream());
    			output.writeBytes(content);
    	        output.flush();
    	        output.close();
    
    		} catch (MalformedURLException e) { //error if internet connection is lost
    			System.out.println("Sounds to me like you lost the internet while the program was running... TOO BAD T_T");
    			return;
    		} catch (IOException e) { //I/O error
    			e.printStackTrace();
    			System.out.println("Looks like Joe doesn't know how to program because you just ran into an I/O exception");
    			return;
    		}
    		
    		//System.out.println(webregConnection.getInputStream());
    		//Prints the output
            BufferedReader bufferedReader = null;
            try
            {
                // Prepare a reader to read the response from the URLConnection
                // throws IOException
                bufferedReader = new BufferedReader(new InputStreamReader(webregConnection.getInputStream()));
                String responeLine;
     
                // Read untill there is nothing left in the stream
                // throws IOException
                while ((responeLine = bufferedReader.readLine()) != null)
                {
                    System.out.println(responeLine);
                }
     
            }
            catch(IOException ioException)
            {
                System.out.println("Problems while reading the response");
                ioException.printStackTrace();
     
                // throw the exception so that the caller is aware that
                // there was some problems
                throw ioException;
     
            }
            finally
            {
                // Good practice: clean up the connections and streams
                // to free up any resources if possible
                if (bufferedReader != null)
                {
                    try
                    {
                        // throws IOException
                        bufferedReader.close();
                    }
                    catch(Throwable ignore)
                    {
                        // Cannot do much with exceptions doing clean up
                        // Ignoring all exceptions
                    }
                }
     
            }
    
    		
    	}
    
    }

  2. #2
    kevinpeter Guest

    Default

    Here's a simple form that includes labels, radio buttons, and push buttons (reset the form or submit it):

    <FORM action="http://somesite.com/prog/adduser" method="post">
    <P>
    <LABEL for="firstname">First name: </LABEL>
    <INPUT type="text" id="firstname"><BR>
    <LABEL for="lastname">Last name: </LABEL>
    <INPUT type="text" id="lastname"><BR>
    <LABEL for="email">email: </LABEL>
    <INPUT type="text" id="email"><BR>
    <INPUT type="radio" name="sex" value="Male"> Male<BR>
    <INPUT type="radio" name="sex" value="Female"> Female<BR>
    <INPUT type="submit" value="Send"> <INPUT type="reset">
    </P>
    </FORM>

Similar Threads

  1. POST information to html form via java?
    By alacn in forum New To Java
    Replies: 7
    Last Post: 08-20-2010, 07:01 AM
  2. where to post questions about javascript in html?
    By iansane in forum Forum Lobby
    Replies: 2
    Last Post: 05-26-2010, 05:12 PM
  3. Replies: 3
    Last Post: 05-04-2009, 04:15 AM
  4. First post out of the way..
    By sirwiggles in forum Introductions
    Replies: 0
    Last Post: 02-06-2009, 11:44 PM
  5. How can I include a html file in html textarea?
    By surya_dks in forum New To Java
    Replies: 2
    Last Post: 10-04-2008, 08:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •