HTML Post : Web Server Client « Network Protocol « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Collections Data Structure
8. Database SQL JDBC
9. Design Pattern
10. Development Class
11. Email
12. Event
13. File Input Output
14. Game
15. Hibernate
16. J2EE
17. J2ME
18. JDK 6
19. JSP
20. JSTL
21. Language Basics
22. Network Protocol
23. PDF RTF
24. Regular Expressions
25. Security
26. Servlets
27. Spring
28. Swing Components
29. Swing JFC
30. SWT JFace Eclipse
31. Threads
32. Tiny Application
33. Velocity
34. Web Services SOA
35. XML
Microsoft Office Word 2007 Tutorial
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Java » Network Protocol » Web Server ClientScreenshots 
HTML Post

//File: PostTest.properties
/*

URL=http://www.java2s.com

*/

//Zip.properties

/*

URL=http://www.java2s.com/product/demo.zip


*/

/**
 @version 1.00 1999-08-28
 @author Cay Horstmann
 */

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Properties;

public class PostTest {
  public static void main(String[] args) {
    try {
      String fileName;
      if (args.length > 0)
        fileName = args[0];
      else
        fileName = "PostTest.properties";
      Properties props = new Properties();
      FileInputStream in = new FileInputStream(fileName);
      props.load(in);

      URL url = new URL(props.getProperty("URL"));
      props.remove("URL");
      String r = doPost(url, props);
      System.out.println(r);
    catch (IOException exception) {
      System.out.println("Error: " + exception);
    }
  }

  public static String doPost(URL url, Properties nameValuePairs)
      throws IOException {
    URLConnection connection = url.openConnection();
    connection.setDoOutput(true);

    PrintWriter out = new PrintWriter(connection.getOutputStream());

    Enumeration e = nameValuePairs.keys();

    while (e.hasMoreElements()) {
      String name = (Stringe.nextElement();
      String value = nameValuePairs.getProperty(name);
      char ch;
      if (e.hasMoreElements())
        ch = '&';
      else
        ch = '\n';
      out.print(name + "=" + URLEncoder.encode(value+ ch);
    }

    out.close();

    BufferedReader in;
    try {
      in = new BufferedReader(new InputStreamReader(connection
          .getInputStream()));
    catch (FileNotFoundException exception) {
      InputStream err = ((HttpURLConnectionconnection).getErrorStream();
      if (err == null)
        throw exception;
      in = new BufferedReader(new InputStreamReader(err));
    }
    StringBuffer response = new StringBuffer();
    String line;

    while ((line = in.readLine()) != null)
      response.append(line + "\n");

    in.close();
    return response.toString();
  }
}
           
       
Related examples in the same category
1. Java HTTP/HTTPS Server Based on new io
2. Connect with a Web serverConnect with a Web server
3. Reading URLs Protected with HTTP Authentication
4. Reading Web Pages with StreamsReading Web Pages with Streams
5. Reading Web Pages, with Socket ChannelsReading Web Pages, with Socket Channels
6. Reading Web Pages with Nonblocking ChannelsReading Web Pages with Nonblocking Channels
7. A Simple Web ServerA Simple Web Server
8. Thread Unsynch
9. Web server
10. Save binary file from web
w___w__w_.__j___a__v_a_2s__.c___om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.