Web access on non-dialup


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Web access on non-dialup

  1. #1
    Join Date
    Oct 2002
    Posts
    12

    Web access on non-dialup

    Alright, I'm designing something that needs to have internet access. The program works wonderfully on dialup, but go to any form of internet that accesses it's information over a LAN and it dies from a connection error. Is there something I'm not doing clientside on IE/Mozilla? can Java access the internet over a LAN? Has someone already designed something that allows for non-dialup web transfer in Java? and if so, where is it?

    Thanks for your help!

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    huh? there's no difference. Java just uses TCP/IP, it doesnt care what the link layer transport is.. you need to give more information on what youre actually doing, because at the moment it sounds like your lan card or settings ont he networked computer are incorrect
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  3. #3
    Join Date
    Oct 2002
    Posts
    12
    *does a basic test* wow, I feel stupid... it seems to just be a problem with the HTTPClient class then....

    Basically, I'm writing a java gui front for a PHPChat and this is the class I'm using for the information transfer... it's never worked on any form of network I've been on... However, it worked perfectly on dialup
    Code:
    import java.util.*;
    import java.io.*;
    import java.net.*;
    
    public class HttpClient
    {
      protected URL url;
      protected HttpURLConnection server;
    
      /**
       * @param szUrl: String object for the URL
       */
      public HttpClient(String szUrl) throws Exception
      {
        try
        {
          url = new URL(szUrl);
        }
        catch (Exception e)
        {
          throw new Exception("Invalid URL");
        }
      }
    
      /**
       * @param method: String object for client method (POST, GET,...)
       */
      public void connect(String method) throws Exception
      {
        try
        {
          server = (HttpURLConnection)url.openConnection();
          server.setDoInput(true);
          server.setDoOutput(true);
          server.setRequestMethod(method);
          server.setRequestProperty("Content-type",
                                    "application/x-www-form-urlencoded");
          server.connect();
        }
        catch (Exception e)
        {
          throw new Exception("Connection failed");
        }
      }
    
      public void disconnect()
      {
        server.disconnect();
      }
    
      public String displayResponse() throws Exception
      {
        String line;
        String return_="";
    
        try
        {
          BufferedReader s = new BufferedReader(
                                new InputStreamReader(
                                    server.getInputStream()));
          line = s.readLine();
          while (line != null)
          {
    		  return_+=line+"\n";
            //System.out.println(line);
            line = s.readLine();
          }
          s.close();
        }
        catch(Exception e)
        {
          throw new Exception("Unable to read input stream");
        }
        return return_;
      }
    
      public void post(String s) throws Exception
      {
        try
        {
          BufferedWriter bw = new BufferedWriter(
                                    new OutputStreamWriter(
                                        server.getOutputStream()));
          bw.write(s, 0, s.length());
          bw.flush();
          bw.close();
        }
        catch(Exception e)
        {
          throw new Exception("Unable to write to output stream");
        }
      }
    
      public static void main(String argv[])
      {
        if (argv.length == 0)
        {
            System.out.println("Usage: java HttpClient url\r\n");
            System.exit(0);
        }
    
        try
        {
          HttpClient c = new HttpClient(argv[0]);
          c.connect("GET");
          c.displayResponse();
          c.disconnect();
    
          c.connect("POST");
          c.post("data=Posted request");
          c.displayResponse();
          c.disconnect();
    
          c.connect("POST");
          c.post("data=2nd request");
          c.displayResponse();
          c.disconnect();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    }
    and if you're wondering, yes, that was taken off the internet.... although I can't exactly remember the source

  4. #4
    Join Date
    Feb 2004
    Posts
    808
    It's not a problem from my lan, though your code has some curious design quirks:

    C:\javawork\trash>java HttpClient http://www.google.com
    Getting
    Posting1
    java.io.IOException: Server returned HTTP response code: 501 for URL: http://www.google.com
    at sun.net.http://www.protocol.http.HttpURLConn...getInputStream(Unknown Source)
    at HttpClient.displayResponse(HttpClient.java:58)
    at HttpClient.main(HttpClient.java:113)
    Posting2
    java.io.IOException: Server returned HTTP response code: 501 for URL: http://www.google.com
    at sun.net.http://www.protocol.http.HttpURLConn...getInputStream(Unknown Source)
    at HttpClient.displayResponse(HttpClient.java:58)
    at HttpClient.main(HttpClient.java:119)

    C:\javawork\trash>java HttpClient http://www.microsoft.com
    Getting
    Posting1
    Posting2
    Code:
    
    import java.util.*;
    import java.io.*;
    import java.net.*;
    
    public class HttpClient
    {
      protected URL url;
      protected HttpURLConnection server;
    
      /**
       * @param szUrl: String object for the URL
       */
      public HttpClient(String szUrl) throws Exception
      {
        try
        {
          url = new URL(szUrl);
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    
      /**
       * @param method: String object for client method (POST, GET,...)
       */
      public void connect(String method) throws Exception
      {
        try
        {
          server = (HttpURLConnection)url.openConnection();
          server.setDoInput(true);
          server.setDoOutput(true);
          server.setRequestMethod(method);
          //server.setRequestProperty("Content-type",
          //                          "application/x-www-form-urlencoded");
          server.connect();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    
      public void disconnect()
      {
        server.disconnect();
      }
    
      public String displayResponse() throws Exception
      {
        String line;
        String ret="";
    
        try
        {
          BufferedReader s = new BufferedReader(
                                new InputStreamReader(
                                    server.getInputStream()));
          line = s.readLine();
          while (line != null)
          {
          ret+=line+"\n";
            //System.out.println(line);
            line = s.readLine();
          }
          s.close();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
        return ret;
      }
    
      public void post(String s) throws Exception
      {
        try
        {
          BufferedWriter bw = new BufferedWriter(
                                    new OutputStreamWriter(
                                        server.getOutputStream()));
          bw.write(s, 0, s.length());
          bw.flush();
          bw.close();
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
      }
    
      public static void main(String argv[])
      {
        if (argv.length == 0)
        {
            System.out.println("Usage: java HttpClient url\r\n");
            System.exit(0);
        }
    
        try
        {
          HttpClient c = new HttpClient(argv[0]);
          System.out.println("Getting");
          c.connect("GET");
          c.displayResponse();
          c.disconnect();
    
          System.out.println("Posting1");
          c.connect("POST");
          c.post("data=Posted request");
          c.displayResponse();
          c.disconnect();
    
          System.out.println("Posting2");
          c.connect("POST");
          c.post("data=2nd request");
          c.displayResponse();
          c.disconnect();
        }
        catch (Exception e)
        {
          e.printStackTrace();
        }
      }
    }
    You catch an exception then throw it again? This is nigh on pointless.. if you do not wish to do naything when an error occurs, do not catch it. if you wish to handle it there and then, in 100% completeness, then handle it.. Do not catch then throw, unless you wish to change the type of exception.. e.g. catch IOException, throw new SukuriException. There are only specific cases where this is necessary..

    Additionally, your error messages that you placed into the new exceptions, were not much help.. they were very generic and any failure was reported as "error reading stream" when actually a far more accurate response of "HTTP 501 - Method Not Implemented" was returned..

    try to avoid C++isms like:
    String return_=""

    in java, this looks ugly; we arent used to looking at nasty variable names with trailing underscores..

    You werent very verbose in your test harness.. i ran the app against microsoft.com for a while, and nothing happened.. i added some printlns and found it to be working as expected, but with no output :)

    the Content-Type header may not be specified as part of a request, only as part of a response. If you send it, the server will ignore it. For a full list of HTTP/1.1 request headers that your request may send, see page 38 of RFC2616: ftp://ftp.isi.edu/in-notes/rfc2616.txt
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  5. #5
    Join Date
    Oct 2002
    Posts
    12
    I believe I said I took that from the internet and was just using it. I find it quite humorous that it was working partially... I'll look over it again and re-write all the confusing and ignorant catch/throws statements. Maybe I should have done this in the first place

  6. #6
    Join Date
    Feb 2004
    Posts
    808
    Originally posted by sukuri
    I believe I said I took that from the internet and was just using it..
    where?

    ps; if youre going to get really into this, dump that junk code and use HTTPUnit
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

  7. #7
    Join Date
    Oct 2002
    Posts
    12
    Originally posted by sukuri
    and if you're wondering, yes, that was taken off the internet.... although I can't exactly remember the source
    ^-- there, under all the code, but yeah.

    I can assume HTTPUnit is the real, professional version of what was being written with all the frills expected from something designed to do everything?

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center