?
Solved

Sending a Url and receiving

Posted on 2000-01-27
21
Medium Priority
?
319 Views
Last Modified: 2013-11-24
I want to write a Java program which opens a socket to send a URL and gets the response - i.e. contents of web site
which I can then Output

can anyone show me i quick example of how to do this?

0
Comment
Question by:plaskowj
  • 10
  • 8
  • 2
  • +1
21 Comments
 
LVL 7

Expert Comment

by:vladi21
ID: 2392959
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2392964
0
 

Author Comment

by:plaskowj
ID: 2393097
This great, but I am still looking for a simple bit of code -

I will make it worth your while.

All it has to do is send a URL and receive the output-


please help
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 7

Expert Comment

by:Ravindra76
ID: 2393232

Hi,

Now this program prints to console.
YOu can write to file if you want

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

public class GetInfo {

      public static void main(String args[]) throws Exception {

            URL url = new URL("http://www.yahoo.com");
            InputStream in = url.openStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            while ( true ) {
                  String line = br.readLine();
                  if ( line ==null ) break;
                  System.out.println(line);
            }

      }


}
0
 
LVL 1

Accepted Solution

by:
sen_kum earned 1600 total points
ID: 2393238
import java.net.* ;
import java.io.* ;
class Test
{
  public static void main( String args[] ) throws Exception
  {
    String aLine,content ;
    aLine = "" ;
    content = "" ;
    URL url = new URL( "www.urlString.com/index.htm" ) ;
    InputStream in = url.openStream() ;
    BufferedReader dis = new BufferedReader(new InputStreamReader(in));
    while ( ( aLine = dis.readLine() ) != null )
    {
      content = content + aLine ;
    }
    System.out.println( content ) ;
  }
}
0
 

Author Comment

by:plaskowj
ID: 2393258
Seeing that you answered that,

can you also tell me with a brief example if a get Socket to listen on a port, is there anyway of determining the url requested by the User?
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2393274
its from http://tactika.com/realhome/javaht/java-f1.html

So where my points? :)

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

 public class suckURL {
   String aFile;
   String aURL;

   public static void main(String args[]) {
     // GIF  JAVA How-to  at Real's Home
     String url =
       "http://www.tactika.com/realhome/images/";
     suckURL b = new suckURL(url, "jht.gif");
     b.doit();
     }

   suckURL(String u, String s){
     aURL  = u;
     aFile = s;
     }

   public void doit() {
     DataInputStream di = null;
     FileOutputStream fo = null;
     byte [] b = new byte[1];  
       
     try {
       System.out.println("Sucking " + aFile);
       System.out.println("   at " + aURL );
       // input
       URL url = new URL(aURL + aFile);
       URLConnection urlConnection = url.openConnection();
       urlConnection.connect();
       di = new DataInputStream(urlConnection.getInputStream());

       // output
       fo = new FileOutputStream(aFile);

       //  copy the actual file
       //   (it would better to use a buffer bigger than this)
       while(-1 != di.read(b,0,1))
         fo.write(b,0,1);
         di.close();  
         fo.close();                
         }
       catch (Exception ex) {
         System.out.println("Oups!!!");
         ex.printStackTrace();
         System.exit(1);
         }
       System.out.println("done.");  
     }
 
0
 

Author Comment

by:plaskowj
ID: 2393278
Seeing that you answered that,

can you also tell me with a brief example if a get Socket to listen on a port, is there anyway of determining the url requested by the User?
0
 

Author Comment

by:plaskowj
ID: 2393285
I'll send you loads of points if you answer my last comment - cheers
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2393309
U mean server socket?
You must implement some protocol
Maybe u want to write http server?
0
 

Author Comment

by:plaskowj
ID: 2393369
Yes, can you give me pointers,  what i am implmenting an application that has a thread listening ona socket.

Anyhow i want to read off the socket - and retireve the URL requested.

Can you help?
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2393411
better use servlets

What are servlets?

  Servlets are server-side Java applications, as opposed to
  client-side applets or standalone applications. While
  servlets are compatible with many different types of servers,
  typically they are used in web servers, as a replacement for
  CGI scripts or Active-Server Pages (ASP).

  Java servlets offer many advantages over other forms of
  server-side processing. Apart from the obvious (they are
  written in the Java programming language, a big plus after
  all), servlet based applications are far easier to write
  than CGI scripts. There's no need to write code for parsing
  HTTP request parameters, as this code is provided by the
  javax.servlet.http package. You have access to the entire
  Java API, with no networking restrictions (making servlets
  far more attractive than applets). There are also performance
  increases over CGI scripts, as servlets persist over time,
  and do not create a new process for every connection.

http://www.as400.ibm.com/developer/java/documents/servlet1.html


Purple Servlet FAQ  http://www.purpletech.com/java/servlet-faq/
http://www.newatlanta.com/servlets/servlet-faq.html 
http://www.dmoz.org/Computers/Programming/Languages/Java/Runtime_Environments/Servlets/faq.html

The Servlet home page on the Javasoft website
http://java.sun.com/products/java-server/servlets/
http://www.coolservlets.com/

Servlet Essentials by Stefan Zeiger
http://www.novocode.com/doc/servlet-essentials/

Paul Flavin's Servlet Resources page
http://www.frontiernet.net/~imaging/servlets_intro.html
http://oop.rosweb.ru/cetus/oo_java_servlets.html
http://dmoz.org/Computers/Programming/Languages/Java/Runtime_Environments/Servlets/


servlets and JSP
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/

Servlet Central, the Server-Side Java Online Magazine
http://www.servletcentral.com 

A list of Servlet engines and Servlet-enabled Web Servers
http://jserv.javasoft.com/products/java-server/servlets/environments.html

http://www.davidreilly.com/java/java_network_programming/jnpfaq.txt
6.1 What are Java Servlets?
  6.2 What do I need to develop servlets?
  6.3 Where can I get more information on servlets?
  6.4 How does servlet performance compare to applets?
  6.5 How does servlet performance compare to CGI?
  6.6 Should I use single-threaded, or multi-threaded
      servlets?
  6.7 How do I send cookies from a servlet?
  6.8 How do I read browser cookies from a servlet?
  6.9 How do I make cookies expire after a set time period?
  6.10 Why aren't cookies stored by my servlets accessible
       to my CGI scripts or ASP pages?
  6.11 How can I void a cookie, and delete it from the
       browser?
0
 

Author Comment

by:plaskowj
ID: 2393712
I don't want to use servlets, as my application is a proxy server - where a gateway will route all URL's entering a specific port to my application.

So therefore i have to keep get the URL from a socket - please help
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2393746
HTTPServer

import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;

public class HTTPServer extends Thread {
      public final static int DEFAULT_PORT = 80;
      protected int port;
      protected ServerSocket server;
      protected ThreadGroup currentConnections;
      protected TextArea logs;
      protected Vector connections;

      public static void fail(Exception e, String msg) {
            System.err.println(msg + ": " + e);
            System.exit(1);
      }

      public HTTPServer(int port) {
            super("HTTPServer");
            if (port == 0) port = DEFAULT_PORT;
            this.port = port;
            try {
                  server = new ServerSocket(port);
            }
            catch (IOException e) {
                  fail(e, "Exception creating server socket");
            }
            currentConnections = new ThreadGroup("Server Connections");
            Frame f = new Frame("Server Status");
            logs = new TextArea();
            logs.setEditable(false);
            f.add("Center", logs);
            f.resize(400, 200);
            f.show();
            connections = new Vector();
            this.start();
      }

      public void run() {
            try {
                  while (true) {
                        Socket ClientSocket = server.accept();
                        Connection c = new Connection(ClientSocket, currentConnections, 3, logs);
                        synchronized(connections) {
                              connections.addElement(c);
                              logs.appendText(c.getInfo() + "\n");
                        }
                  }
            }
            catch (IOException e) {
                  fail(e, "Exception while listening for connections");
            }
      }

      public static void main(String args[]) {
            int port = 0;
            if (args.length == 1) {
                  try {
                        port = Integer.parseInt(args[0]);
                  }
                  catch (NumberFormatException e) {
                        port = 0;
                  }
            }
            new HTTPServer(port);
      }
}

class Connection extends Thread {
      static int numberOfConnections = 0;
      protected Socket client;
      protected DataInputStream in;
      protected PrintStream out;
      private TextArea logs;

      public Connection(Socket clientsocket, ThreadGroup currentconnections, int priority, TextArea logs) {
            super(currentconnections, "Connection number" + numberOfConnections++);
            this.setPriority(priority);
            client = clientsocket;
            try {
                  in = new DataInputStream(client.getInputStream());
                  out = new PrintStream(client.getOutputStream());
            }
            catch (IOException e) {
                  try {
                        client.close();
                  }
                  catch (IOException e2) {}
                  System.err.println("Exception while getting socket streams: " + e);
                  return;
            }
            this.logs = logs;
            this.start();
      }

      public void run() {
            String inline;
            String header, request = "";
            String currentdir = System.getProperty("user.dir");
            try {
                  while(true) {
                        inline = in.readLine();
                        request += inline + "\n";
                        if (inline.length() == 0) {
                              logs.appendText(request + "\n");
                              StringTokenizer parse = new StringTokenizer(request);
                              if ((parse.nextToken()).equalsIgnoreCase("GET")) {
                                    inline = parse.nextToken();
                                    FileInputStream f = null;
                                    try {
                                          f = new FileInputStream(currentdir + inline);
                                    }
                                    catch (FileNotFoundException e) {
                                          out.println("File " + inline + " not found");
                                          break;
                                    }
                                    header  = "HTTP/1.1 200 Document follows\n";
                                    header += "Date: " + (new Date()).toGMTString() + "\n";
                                    header += "Server: PlaQ Server\n";
                                    File info = new File(currentdir + inline);
                                    header += "Last-modified: " + (new Date(info.lastModified())).toGMTString() + "\n";
                                    header += "Content-type: ";
                                    String name = info.getName();
                                    logs.appendText("File: " + name + "\n");
                                    int idx = name.indexOf(".");
                                    name = name.substring(idx + 1);
                                    if (name.equalsIgnoreCase("html") || name.equalsIgnoreCase("htm"))
                                          header += "text/html\n";
                                    else if (name.equalsIgnoreCase("jpeg") || name.equalsIgnoreCase("jpg"))
                                          header += "image/jpeg\n";
                                    else if (name.equalsIgnoreCase("gif"))
                                          header += "image/gif\n";
                                    else
                                          header += "text/plain\n";
                                    header += "Content-length: " + info.length() + "\n";
                                    header += " ";
                                    logs.appendText(header);
                                    out.println(header);
                                    out.flush();
                                    byte[] b = new byte[4096];
                                    int len;
                                    long bytestrasnfered = 0;
                                    try {
                                          while ((len = f.read(b)) != -1) {
                                                bytestrasnfered += len;
                                                out.write(b, 0, len);
                                          }
                                    }
                                    catch (IOException e) {}
                                    f.close();
                                    out.flush();
                                    logs.appendText("Transfered " + bytestrasnfered + " bytes\n");
                                    request = "";
                              }
                              else {
                                    out.println("Unknown command");
                              }
                        }
                  }
                  logs.appendText("End of while\n");
            }
            catch (IOException e) {}
            finally {
                  try {
                        logs.appendText("Close " + getInfo() + "\n");
                        client.close();
                  }
                  catch (IOException e2) {}
            }
      }

      public String getInfo() {
            return ("Connection from: " + client.getInetAddress().getHostName());
      }
}


----
Is your application  a  HTTP proxy server ?
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2393785
look
http://www.experts-exchange.com/jsp/qShow.jsp?ta=browsers&qid=10166511 

Just parse first string of HTTP request

example in Java:

if ( frame.useProxyCheckBox.getState() )
{
remoteHost=frame.proxyHost;
remotePort=frame.proxyPort;
}else
{
int second;
int first=request.indexOf(' ');
if (first>0)
{
second=request.indexOf(' ', first+1);
if (second>0)
{
//System.out.println("\n\nrequestURL="+request.substring(first+1,second)+"_____");
URL requestURL=new URL(request.substring(first+1,second));
remoteHost=requestURL.getHost();
remotePort=requestURL.getPort();
if (remotePort==-1) remotePort=80;
System.out.println("path="+requestURL.getFile());

request=request.substring(0,first+1)
+requestURL.getFile()
+request.substring(second);

}
//zzzzz
}
}


0
 
LVL 7

Expert Comment

by:vladi21
ID: 2393797
if u want I can mail to you my sources of simple proxy server

Post new Q with lot of points and ur e-mail :)
0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2395418

I did n't find any difference between my comment and proposed answer.
0
 

Author Comment

by:plaskowj
ID: 2463042
vladi 21-

I can't seem to browse to this through an Internet browser

why?

can you help?
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2463132
plaskowj : what a problem?
0
 

Author Comment

by:plaskowj
ID: 2463228
can you help?

i can set up a Client java application which can access this port, but i can't do it through a browser-

please help
0
 
LVL 7

Expert Comment

by:vladi21
ID: 2463303
which port?

post new Q with description
and where my points ? :)
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month7 days, 6 hours left to enroll

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question