Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Applets & HTTP

Posted on 2001-06-27
13
Medium Priority
?
189 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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 1200 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

824 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