www.webdeveloper.com
Results 1 to 3 of 3

Thread: Connecting to the web

  1. #1
    Join Date
    Mar 2006
    Posts
    3

    Unhappy Connecting to the web

    I tried the example given in java tech tips. But It always throws the exception "UnknownhostException" though I set the properties

    System.setProperty("http.proxyHost","webcache");
    System.setProperty("http.proxyPort","8080");

    System.setProperty("https.proxyHost","webcache");
    System.setProperty("https.proxyPort","8080");

    I am using proxy setup for accessing the net.



    import java.net.URL;
    import java.net.MalformedURLException;
    import java.net.URLConnection;
    import java.io.IOException;
    import java.io.BufferedReader;
    import java.io.InputStreamReader;

    public class WebPageReader {

    private static URLConnection connection;

    private static void connect( String urlString ) {
    try {
    URL url = new URL(urlString);
    connection = url.openConnection();
    } catch (MalformedURLException e){
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    private static void readContents() {
    BufferedReader in = null;
    try {
    in = new BufferedReader(
    new InputStreamReader(
    connection.getInputStream()));

    String inputLine;
    while (
    (inputLine = in.readLine()) != null) {
    System.out.println(inputLine);
    }
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public static void main(String[] args) {
    if (args.length != 1) {
    System.err.println("usage: java WebPageReader "
    + "<url>");
    System.exit(0);
    }
    connect(args[0]);
    readContents();
    }
    }


    Thanks

  2. #2
    Join Date
    Mar 2006
    Posts
    6

    Reading a file from the web

    I m not sure what you are trying to do, but following is a listing of a class that reads a csv file from the uk lottery site and simply shows it on the console.


    import java.io.BufferedInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;

    /**
    * @version 1.0
    * @author
    */
    public class LoadFile2 {

    public void loadFile(String inURL) throws IOException {

    URL theURL = null;
    try {
    theURL = new URL(inURL);
    } catch (MalformedURLException e) {
    System.out.println("Bad URL: " + theURL);
    }

    URLConnection conn = null;
    DataInputStream data = null;
    String line;

    try {
    conn = theURL.openConnection();
    conn.connect();

    System.out.println("Connection opened...");

    data =
    new DataInputStream(
    new BufferedInputStream(conn.getInputStream()));

    System.out.println("Reading data...");
    while ((line = data.readLine()) != null) {
    System.out.println(line);
    }

    data.close();
    } catch (IOException e) {
    System.out.println("IO Error:" + e.getMessage());
    }
    }
    /** Main. */
    public static void main(String[] args) {
    LoadFile2 loadFile2 = new LoadFile2();
    try {
    loadFile2.loadFile("http://www.national-lottery.co.uk/player/files/Lotto.csv");
    } catch (IOException e) {
    e.printStackTrace();
    }

    }
    }

  3. #3
    Join Date
    Mar 2006
    Posts
    6
    Well I had to make a connection to the server project for my class, I don't know if this could help but here it is my code:


    WebClient.JAVA:

    import java.io.*;
    import java.net.*;

    /**
    * Receives a String from WebClientTester and converts it to URL type, connects
    * to the server and checks whether is a correct or incorrect page.
    */
    public class WebClient
    {
    //addr1 - a place holder for the address received by WebClientTester.java.
    private String addr1;

    //u - a place holder for the addr1 once it has been converted from String to URL type.
    private URL u;

    //huc - a place holder for the object that represents a connection to the URL.
    private HttpURLConnection huc;

    //finalResponse - the return value of the getResponse method.
    private String finalResponse;

    //Constructor WebClient, creates a new object of the WebClient Class.
    public WebClient()
    {
    addr1=null;
    }

    /**
    * Receives a String from WebClientTester converts it to URL type.
    * To indicate the URL of the WebPage.
    *
    * @param addr Address received from WebClientTester but still String type.
    * @throws MalformedURLException
    */
    void setURL(String addr) throws MalformedURLException
    {
    addr1=addr;
    u = new URL(addr1);
    }

    /**
    * Gets the Server's Answer for the address given.
    *
    * @return finalResponse Returns the server's response of type String.
    */
    String getResponse()
    {
    try
    {
    huc = (HttpURLConnection) u.openConnection();
    finalResponse="HTTP/1.0 "+" "+huc.getResponseCode()+" "+huc.getResponseMessage();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }

    return finalResponse;
    }


    }

    WebClientTester.java

    import java.io.*;
    import java.net.*;
    import javax.swing.*;

    /**
    *Prints the Intro and the instructions for the user. Stores the user's input.
    *Sends all the addresses to the callsWebClient method in order to eliminate code lines.
    */
    public class WebClientTester
    {

    /**
    * @param args
    * @throws IOException
    */

    public static void main(String[] args) throws IOException
    {
    WebClient address=new WebClient();

    String addr1 ="http://www.google.com/";
    String addr2 = "http://www.uprm.edu/";
    String addr3 = "http://www.hotmail.com/";

    //Graphical application that displays a frame window with a WELCOME.
    JFrame window = new JFrame();
    JLabel windowIcon = new JLabel(new ImageIcon("ComputerIcon.gif"));
    JLabel windowText = new JLabel(" WELCOME TO THE PAGE-TESTER PROGRAM ");

    JPanel panel= new JPanel();
    panel.add(windowIcon);
    panel.add(windowText);
    window.setContentPane(panel);

    window.pack();
    window.show();


    //Three correct addresses
    System.out.println("* EXAMPLES OF CORRECT PAGES WITH CORRECT SERVERS *");
    System.out.println();

    System.out.println(addr1);
    callsWebClient(addr1,address);

    System.out.println(addr2);
    callsWebClient(addr2,address);

    System.out.println(addr3);
    callsWebClient(addr3,address);

    //Three incorrect addresses
    System.out.println();
    System.out.println("* EXAMPLES OF INCORRECT PAGES WITH CORRECT SERVERS *");
    System.out.println();

    System.out.println(addr1+"dfdcss");
    callsWebClient(addr1+"dfdcss",address);

    System.out.println(addr2+"dfdcss");
    callsWebClient(addr2+"dfdcss",address);

    System.out.println(addr3+"dfdcss");
    callsWebClient(addr3+"dfdcss",address);

    System.out.println();
    System.out.println("----------------------------------------------------------------------------------------");
    System.out.println(" ENTER A PAGE THAT YOU WOULD LIKE TO TEST WHETHER IT WORKS OR NOT");
    System.out.println("----------------------------------------------------------------------------------------");
    System.out.println();
    System.out.println(" * PLEASE MAKE SURE TO FOLLOW THE FOLLOWING DESCRIPTION: http://ADDRESS_GOES_HERE *");
    System.out.println(" ** NOTE: THE SERVER MUST BE CORRECT BUT THE PAGE INCORRECT OR I WILL THROW AN **");
    System.out.println(" *** EXCEPTION ***");

    //Reads a line of input and stores it in "input". Calls the callsWebClient method.
    BufferedReader console=new BufferedReader(new InputStreamReader(System.in));
    String input=console.readLine();
    callsWebClient(input,address);


    }

    /**
    * Prints the the response given from the server.
    * Calls the getResponse and setURL methods from WebClient.java.
    * It will be called from main method using a single line.
    *
    * @param addr String received from main that contains the address.
    * @param address Needed to call the WebClient.java methods.
    * @throws MalformedURLException
    */
    public static void callsWebClient(String addr,WebClient address) throws MalformedURLException
    {
    address.setURL(addr);
    System.out.println(address.getResponse());
    System.out.println();
    }
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles