//File: PostTest.properties
/*
URL=http://www.java2java.com
*/
//Zip.properties
/*
URL=http://www.java2java.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 = (String) e.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 = ((HttpURLConnection) connection).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();
}
}
|