Results 1 to 5 of 5

Thread: URLConnection

  1. #1
    warrier is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default URLConnection

    Hey I'm relatively new to networking in Java but i have created some client/server programs. However, in this program im trying to post to a URL. i want the user to enter a word, and that word to be searched up in dictionary.reference.com and the results to be sent back. here is my code:

    Java Code:
    package networking;
    
    import java.net.*;
    import java.io.*;
    import java.util.Scanner;
    
    public class Website {
    	static Scanner iScan = new Scanner(System.in);
    	public static void main(String args[]) throws Exception
    	{
    		URL dict = new URL("http://www.dictionary.reference.com/");
    		URLConnection yc = dict.openConnection();
    		yc.setDoOutput(true);
    		
    		System.out.println("Word: ");
    		String word = iScan.nextLine();
    		word = URLEncoder.encode(word, "UTF-8");
    		
    		
    		OutputStreamWriter out = new OutputStreamWriter(yc.getOutputStream());
    		out.write("q=" + word);
    		out.close();
    		
    		BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
    		String inputLine;
    		while ((inputLine = in.readLine()) != null)
    			System.out.println(inputLine);
    		in.close();
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: URLConnection

    And?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    warrier is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: URLConnection

    and it doesnt work and it outputs the websites HTML code

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default Re: URLConnection

    Then I'd look at what the website is expecting.
    Sounds like it's not expecting a simple post of a word.

    Is there some documentation?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    warrier is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: URLConnection

    i looked at the source code to find out the id of the search which was "q"
    and theres no documentation but i contacted the websites owners

Similar Threads

  1. URLConnection
    By tnrh1 in forum New To Java
    Replies: 9
    Last Post: 12-20-2011, 03:03 PM
  2. UrlConnection question(s)
    By dhazwa in forum New To Java
    Replies: 1
    Last Post: 08-31-2011, 10:56 AM
  3. Lock between URLConnection and System.in
    By mab in forum Networking
    Replies: 1
    Last Post: 02-24-2011, 01:21 PM
  4. URLConnection Efficiency
    By Lil_Aziz1 in forum New To Java
    Replies: 22
    Last Post: 08-19-2010, 07:27 PM
  5. Opening URLConnection
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-24-2007, 08:37 PM

Tags for this Thread

Posting Permissions

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