Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2.  
  3. Copyright (c) Non, Inc. 1997 -- All Rights Reserved
  4.  
  5. PROJECT: JavaWorld
  6. MODULE: Web Stuff
  7. FILE: Happy.java
  8.  
  9. AUTHOR: John D. Mitchell, Jul 8, 1997
  10.  
  11. REVISION HISTORY:
  12. Name Date Description
  13. ---- ---- -----------
  14. JDM 97.07.08 Initial version.
  15.  
  16. DESCRIPTION:
  17.  
  18. This file shows how to POST to a web-server and get back the raw
  19. response data.
  20.  
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  22.  
  23.  
  24. import java.awt.*;
  25. import java.applet.*;
  26. import java.io.*;
  27. import java.util.*;
  28. import java.net.*;
  29.  
  30.  
  31. public class Happy extends Applet
  32. {
  33. private TextArea textArea = new TextArea (25, 70);
  34.  
  35. public void init ()
  36. {
  37. try
  38. {
  39. URL url;
  40. URLConnection urlConn;
  41. DataOutputStream printout;
  42. DataInputStream input;
  43.  
  44. // URL of CGI-Bin script.
  45. url = new URL (getCodeBase().toString() + "env.cgi");
  46.  
  47. // URL connection channel.
  48. urlConn = url.openConnection();
  49.  
  50. // Let the run-time system (RTS) know that we want input.
  51. urlConn.setDoInput (true);
  52.  
  53. // Let the RTS know that we want to do output.
  54. urlConn.setDoOutput (true);
  55.  
  56. // No caching, we want the real thing.
  57. urlConn.setUseCaches (false);
  58.  
  59. // Specify the content type.
  60. urlConn.setRequestProperty
  61. ("Content-Type", "application/x-www-form-urlencoded");
  62.  
  63. // Send POST output.
  64. printout = new DataOutputStream (urlConn.getOutputStream ());
  65.  
  66. String content =
  67. "name=" + URLEncoder.encode ("Buford Early") +
  68. "&email=" + URLEncoder.encode ("buford@known-space.com");
  69.  
  70. printout.writeBytes (content);
  71. printout.flush ();
  72. printout.close ();
  73.  
  74. // Get response data.
  75. input = new DataInputStream (urlConn.getInputStream ());
  76.  
  77. String str;
  78. while (null != ((str = input.readLine())))
  79. {
  80. System.out.println (str);
  81. textArea.appendText (str + "\n");
  82. }
  83.  
  84. input.close ();
  85.  
  86. // Display response.
  87. add ("Center", textArea);
  88. }
  89. catch (MalformedURLException me)
  90. {
  91. System.err.println("MalformedURLException: " + me);
  92. }
  93. catch (IOException ioe)
  94. {
  95. System.err.println("IOException: " + ioe.getMessage());
  96. }
  97. } // End of method init().
  98. } // End of class Happy.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement