Posted By:
Brian_Glodde
Posted On:
Friday, April 2, 2004 11:28 AM
You may be running into trouble trying to write output to a server that doesn't allow it. For example, an HTTP response code of 501 would indicate you cannot post in this manner (you can't use this on Google, for example)...in short, it won't allow you to use the line
c.doSetOutput(true);. Here is simpler example, this time using a GET to pull back search results from Google.com.
import java.io.*;
import java.net.*;
public class PostHTTP {
public static void main(String args[]) throws IOException,
UnknownHostException {
try {
URL url = new URL(
"http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=http+post+url+java");
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible;MSIE 5.5; Windows NT 5.0; H010818)");
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.
getInputStream()));
String line;
while ( (line = rd.readLine()) != null) {
System.out.println(line);
}
rd.close();
}
catch (Exception e) {
System.out.println(e.toString());
}
}
}