// // UrlPostConnection class // part of the set of documents known as Java no sugar. // Copyright (c) 1996 Sunil Gupta, sunil@magnetic.demon.co.uk // placed into the public domain by the author // // loosely based on Cgi.class written by Jim Driscoll http://www.io.com/~maus // functional interface tidied by sunil@magnetic.demon.co.uk // the original had a confused fucntional interface,held onto // sockets and streams longer than neccessary, and didnt // return the output data. // // returns a document object. // import java.net.*; import java.io.*; import java.util.*; import Document; public class UrlPostConnection { //********************************************************************* // Globals //********************************************************************* static final int DEFAULT_PORT = 80; static final String POST = "POST"; static final boolean DEBUG = false; //********************************************************************* // instance variables //********************************************************************* protected String method = null; protected String host_name = null; protected int httpd_port = DEFAULT_PORT; protected String cgi_path = null; protected Properties properties = null; //********************************************************************* // constructors //********************************************************************* public UrlPostConnection( String hostid, String path) { this(hostid, DEFAULT_PORT, path); } //********************************************************************* public UrlPostConnection( String hostid, String port, String path) throws NumberFormatException { this(hostid, Integer.parseInt(port), path); } //********************************************************************* public UrlPostConnection( String hostid, int port, String path) { host_name = hostid; httpd_port = port; cgi_path = path; method = POST; } //********************************************************************* // other stuff //********************************************************************* public Document sendData() throws UnknownHostException, IOException { Socket sock = null; DataInputStream reply = null; PrintStream send = null; String dataString = encode_properties(); String reply_string; Document output_doc; //--------------open the socket---------------------------------- sock = new Socket( host_name, httpd_port ); send = new PrintStream( sock.getOutputStream() ); reply = new DataInputStream( sock.getInputStream() ); //--------------send the data------------------------------------- try { send.print(method + " " + cgi_path + " HTTP/1.0\r\n"); send.print("Content-type: application/x-www-form-urlencoded\r\n"); send.print("Content-length: " + dataString.length() +"\r\n"); send.print("\r\n"); send.print(dataString); send.print("\r\n"); //--------------get any reply------------------------------------- output_doc = new Document(reply); sock.close(); } catch (IOException e) { sock.close(); throw e; } //--------------close the socket---------------------------------- return(output_doc); } //********************************************************************* public String toString() { return("http://" + host_name + ":" + httpd_port + cgi_path); } //********************************************************************* public void setProperty(String key, String value) { if (properties == null) properties = new Properties(); properties.put(key,URLEncoder.encode(value)); } //********************************************************************* public void clearProperties() { properties = null; } //********************************************************************* // private stuff //********************************************************************* private String encode_properties() { Enumeration property_names; String encoded = new String(); String key, value; boolean first = true; if (properties != null) { property_names = properties.propertyNames(); while (property_names.hasMoreElements()) { if ( !first ) encoded = encoded + "&"; else first = false; key = (String)property_names.nextElement(); value = properties.getProperty(key); encoded = encoded + key + "=" + value; } } return encoded; } }
file: /Techref/language/java/nosugar/comms/code/UrlPostConnection.java, 4KB, , updated: 1996/10/10 10:08, local time: 2024/12/26 07:35,
3.16.130.96:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.ecomorder.com/techref/language/java/nosugar/comms/code/UrlPostConnection.java"> language java nosugar comms code UrlPostConnection</A> |
Did you find what you needed? |
Welcome to ecomorder.com! |
Welcome to massmind.ecomorder.com! |
.