please dont rip this site

Language Java Nosugar Comms Code Urlpostconnection.java

// 
//	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 11:08, local time: 2024/5/19 13:23,
TOP NEW HELP FIND: 
3.144.159.187: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?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://www.piclist.com/techref/language/java/nosugar/comms/code/UrlPostConnection.java"> language java nosugar comms code UrlPostConnection</A>

Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host massmind.org, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
 
Quick, Easy and CHEAP! RCL-1 RS232 Level Converter in a DB9 backshell
Ashley Roll has put together a really nice little unit here. Leave off the MAX232 and keep these handy for the few times you need true RS232!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .