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

Java
C++
PHP
Java Home »  Network Protocol   » [  Web Server Client  ]  Screenshots 
 



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 server Connect with a Web server
2.  Reading URLs Protected with HTTP Authentication
3.  Reading Web Pages with Streams Reading Web Pages with Streams
4.  Reading Web Pages, with Socket Channels Reading Web Pages, with Socket Channels
5.  Reading Web Pages with Nonblocking Channels Reading Web Pages with Nonblocking Channels
6.  A Simple Web Server A Simple Web Server
7.  Thread Unsynch
8.  Web serverHas Download File
9.  Save binary file from web
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.