TheLinuxDuck
06-06-2001, 03:59 PM
I've been playing with connecting to a URL and getting information about that URL. What I would like to do is to create a connection, and actually get the contents of the document connected to.. I assume that this is what the URLConnection.getInput/OutputStream calls are for.
However, I'm feeling a little overwhelmed. The code I'm about to post connects to the URL I give it, sets specs for the connection, establishes a connection, and gets information about the connection. However, the InputStream stuff I tried didn't work first attempt, and my head is starting to feel swimmy from all this new stuff I'm learning, so if someone would be kind enough to help me find my mistakes, and possible alternate methods for what I'm attempting.
What I think would benefit me more is hints and suggestions as to what direction to go... and not necessarily the answer..
Not that I don't want the answer, but I'd like to see if my head will pick this stuff apart, and understand it enough to make some sense of the stuff that doesn't make sense.
Anyhow, without any further adeau-deau:
import java.io.*;
import java.net.*;
public class URLtest1 {
public static void main(String[] args) {
String myString=new String("http://my.url.that/connectto/form.html");
URL myURL;
try {
myURL=new URL(myString);
URLConnection myConnect=myURL.openConnection();
myConnect.setAllowUserInteraction(false);
myConnect.setDoInput(true);
myConnect.setDoOutput(false);
myConnect.setUseCaches(false);
if(myConnect instanceof HttpURLConnection) {
HttpURLConnection myHttp=(HttpURLConnection)myConnect;
myHttp.setRequestMethod("POST");
}
myConnect.connect();
System.out.println("Content type: " + myConnect.getContentType());
System.out.println("Content length: " + myConnect.getContentLength());
if(myConnect instanceof HttpURLConnection) {
HttpURLConnection myHttp=(HttpURLConnection) myConnect;
<STRONG>BufferedReader in=new BufferedReader(new InputStreamReader(myHttp.getInp
utStream()));
String myBuffer;</STRONG>
System.out.println("Request method: " + myHttp.getRequestMethod());
System.out.println("Content:");
<STRONG>do {
myBuffer=in.readLine();
} while(myBuffer!=null);</STRONG>
}
}
catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
I took the liberty of bolding the stuff that I'm unsure about.
Anybody? (^=
However, I'm feeling a little overwhelmed. The code I'm about to post connects to the URL I give it, sets specs for the connection, establishes a connection, and gets information about the connection. However, the InputStream stuff I tried didn't work first attempt, and my head is starting to feel swimmy from all this new stuff I'm learning, so if someone would be kind enough to help me find my mistakes, and possible alternate methods for what I'm attempting.
What I think would benefit me more is hints and suggestions as to what direction to go... and not necessarily the answer..
Not that I don't want the answer, but I'd like to see if my head will pick this stuff apart, and understand it enough to make some sense of the stuff that doesn't make sense.
Anyhow, without any further adeau-deau:
import java.io.*;
import java.net.*;
public class URLtest1 {
public static void main(String[] args) {
String myString=new String("http://my.url.that/connectto/form.html");
URL myURL;
try {
myURL=new URL(myString);
URLConnection myConnect=myURL.openConnection();
myConnect.setAllowUserInteraction(false);
myConnect.setDoInput(true);
myConnect.setDoOutput(false);
myConnect.setUseCaches(false);
if(myConnect instanceof HttpURLConnection) {
HttpURLConnection myHttp=(HttpURLConnection)myConnect;
myHttp.setRequestMethod("POST");
}
myConnect.connect();
System.out.println("Content type: " + myConnect.getContentType());
System.out.println("Content length: " + myConnect.getContentLength());
if(myConnect instanceof HttpURLConnection) {
HttpURLConnection myHttp=(HttpURLConnection) myConnect;
<STRONG>BufferedReader in=new BufferedReader(new InputStreamReader(myHttp.getInp
utStream()));
String myBuffer;</STRONG>
System.out.println("Request method: " + myHttp.getRequestMethod());
System.out.println("Content:");
<STRONG>do {
myBuffer=in.readLine();
} while(myBuffer!=null);</STRONG>
}
}
catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
I took the liberty of bolding the stuff that I'm unsure about.
Anybody? (^=