HTML Post : Web Server Client : Network Protocol : Java examples (example source code) Organized by topic

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. JSP
19. JSTL
20. Language Basics
21. Network Protocol
22. PDF RTF
23. Regular Expressions
24. Security
25. Servlets
26. Spring
27. Swing Components
28. Swing JFC
29. SWT JFace Eclipse
30. Threads
31. Tiny Application
32. Velocity
33. XML
Java Tutorial
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. Connect with a Web serverConnect with a Web server
2. Reading URLs Protected with HTTP Authentication
3. Reading Web Pages with StreamsReading Web Pages with Streams
4. Reading Web Pages, with Socket ChannelsReading Web Pages, with Socket Channels
5. Reading Web Pages with Nonblocking ChannelsReading Web Pages with Nonblocking Channels
6. A Simple Web ServerA Simple Web Server
7. Thread Unsynch
8. Web server
9. Save binary file from web
Home | Contact Us
Copyright 2003 - 07 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.