Click to See Complete Forum and Search --> : java help please! (dru?)


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? (^=

feve
06-06-2001, 05:05 PM
If by contents you mean the HTML behind the page, you can use the shortcut openStream method of the URL class. It's a shortcut for
URL.openConnection().getInputStream().

You should be able to wrap the InputStream returned by that the same way you did the one from HttpURLConnection.


new InputStreamReader(myURL.openStream());


I also noticed that you appear to be overriding whatever is in myBuffer, rather then building it up. I don't know if that's what you want. Maybe, myBuffer += in.readLine(). You can use a similar idea to dump it all to the screen as a test.


String line;

while ((line = in.readLine()) != null) {
System.out.println(line);
}


I've never used the HttpURLConnection class so i'm sorry i can't be of help there.

[ edit ]
Sorry i apparently have reading problems. I posted code to get the HTML, but then noticed your requests for hints rather than the answer so i deleted it. Let me know if youd' like the code. Hope this helps.

[ 06 June 2001: Message edited by: feve ]

feve
06-06-2001, 05:50 PM
Either i'm way off on what you want or you're right there. I just changed your code to print out myBuffer during each pass through the do loop. Ran on www.perl.com (http://www.perl.com) (in your honor, for all the perl help you've given me) and the HTML rolled down screen. The openStream shortcut is just that. The HttpURLConnection class seems to work fine.

Unless of course, i'm way off and you don't want the HTML behind the URL. If instead you want something like user input or some other values i shouldn't have posted, because i have no idea - but what you wrote gets the HTML fine.

[ 06 June 2001: Message edited by: feve ]

TheLinuxDuck
06-06-2001, 10:33 PM
I AM SUCH A DOPE!!!!!!!!!!!!!!!!!!!!!

Heh... I forgot to put the print statement in the read loop!!!!!!!!!!!!!!!

Thanks for pointing out my idiocity!! I can't believe I forgot that! No wonder it didn't print anything out!?!!!!! LOL!!

Ok, so I've once again proved I am a doof.

Feve, thanks! I prolly wouldn't have caught that had it not been for your help.

Qool.. if that works as it should, then I was doing it right. Qool! That's actually very encouraging! Considering what little experience i have with jaba.

Qool, qool, qool.. I'm fairly excited now!

Woowoo!!!

Dru Lee Parsec
06-06-2001, 11:37 PM
Here I am checking LNO and I find that I'm not needed. That's what I get for workin during the day. ;) Glad you discovered the solution LDuck.

TheLinuxDuck
06-07-2001, 12:42 AM
Originally posted by Dru Lee Parsec:
<STRONG>Here I am checking LNO and I find that I'm not needed. That's what I get for workin during the day. ;) Glad you discovered the solution LDuck.</STRONG>

Actually, Feve pointed it out to me.. (^= And don't think that you're not needed.. just because this particular instance was solved, it doesn't mean that I, nor someone else, will not need your help in the future.

Since I've been getting into java more, I'm actually prolly going to yelling out for you more.. (^=

And to think of my level of understanding since your initial light bulb example.. (^=