Solved

Applets & HTTP

Posted on 2001-06-27
13
175 Views
Last Modified: 2013-11-23
I need to write a Java applet that can request pages possibly passing form or query string data to the pages from the server the applet came from.

i need to know if there is a good HTTP class for java, where to get it etc and an example of requesting a page sending form data and then displaying the response on the command line would be nice.

thanx alot.

Paul
0
Comment
Question by:makerp
  • 7
  • 5
13 Comments
 
LVL 5

Expert Comment

by:Calron
ID: 6231046
Have a look at:

http://java.sun.com/docs/books/tutorial/networking/index.html

This tutorial explains the use of the URL class which is exactly what you are looking for. It allows you to post and get data from Http connections.
0
 
LVL 10

Author Comment

by:makerp
ID: 6231060
cool, okay i will leave this open for a few days because i want to get someother links and advice, also i dont intend to start this until next monday. but dont worry i will award the points (to the best answer of course).

(a little example would be nice)

Paul
0
 
LVL 5

Expert Comment

by:Calron
ID: 6231091
One example of reading from a URL (pretty much what the tutorial shows):

It read the whole text from that URL and returns it as a string.

 public static String getPage(String urlString)
  {
    String page = new String();
    try
    {
      URL dest = new URL(urlString);
      BufferedReader in = new BufferedReader(
                     new InputStreamReader(
                     dest.openStream()));

      String inputLine;

      while ((inputLine = in.readLine()) != null)
      {
     page = page + "\n" + inputLine;
      }

      in.close();
    }
    catch (MalformedURLException e)
    {
      return new String("MalformedURLException occurred: " + e.getMessage());
    }
    catch (IOException e)
    {
      return new String("IOException occurred: " + e.getMessage());
    }

    return page;
  }
0
 
LVL 10

Author Comment

by:makerp
ID: 6231106
what about posting data to the page
0
 
LVL 5

Expert Comment

by:Calron
ID: 6231113
And posting to a URL:

public void sendToServer () {
   try {
    URL query = new URL( "http://yourserver.com/abc.cgi " );

    HttpURLConnection conn = ( HttpURLConnection) query.openConnection();

    conn.setRequestMethod( "POST" );
    conn.setDoOutput(true);

    OutputStream os =  conn.getOutputStream();

    /* write to the OutputStream here */

    os.close();

   } catch ( Exception E ) {
     E.printStackTrace();
   }
 }
0
 
LVL 5

Expert Comment

by:Calron
ID: 6231122
The first example could also be solved using the URLConnection class, as this class has the getOuputStream and a getInputStream class.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 5

Expert Comment

by:Calron
ID: 6231146
One thing, if you want to do the same using https protocol you will need JSSE packeage from sun. Once you have that included in you classpath https connections can be made using pretty much the same methods as above.

http://java.sun.com/products/jsse/index.html
0
 

Expert Comment

by:SimonSay
ID: 6233119
Here is a HTTPMessage class that you can use to simplify your production :

http://www.servlets.com/cos/javadoc/com/oreilly/servlet/HttpMessage.html


You can also take a look at the HTTP client source or the applet source in this page as an example.

http://www.servlets.com/jservlet2/examples/ch10/index.html
0
 
LVL 10

Author Comment

by:makerp
ID: 6243979
where can i get JDK 1.new version, i only have 1.1.8, also the http stuff, does this come with it?

Paul

0
 
LVL 5

Expert Comment

by:Calron
ID: 6243991
Try:

http://java.sun.com/j2se/1.3/

to download the JDK 1.3, which is currently the newest version. 1.4 is available as Beta and will be released inb Fall.
0
 
LVL 10

Author Comment

by:makerp
ID: 6244068
does this have all the http stuff i need
0
 
LVL 5

Accepted Solution

by:
Calron earned 300 total points
ID: 6244080
It has all the classes you need unless you are planning to access https connections where you will require the JSSE package, which can be downloaded from:

http://java.sun.com/products/jsse/index.html
0
 
LVL 10

Author Comment

by:makerp
ID: 6244112
thank you for your help and being patient for me to acept your answer, i hope you can help me again sometime
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
The viewer will learn how to implement Singleton Design Pattern in Java.

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now