aspose file tools*
The moose likes I/O and Streams and the fly likes Using the openConnect() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Using the openConnect()" Watch "Using the openConnect()" New topic
Author

Using the openConnect()

von rodrig
Greenhorn

Joined: Jun 28, 2002
Posts: 2
I am trying to use the openConnect() to see if an url exist and plus check to see if there are any error code. My problem is with the check the error code I tried using the getResponseCode and got error message method getResponseCode() not found in class java.net.URLConnection. Can someone help me figure out how to use this? I attached my code:

import java.lang.Object.*;
import java.io.*;
import java.net.*;

public class URLConnTest2 {

public static void main (String args[]) {
try {
//Url to try to connect to
URL u1 = new URL("http://199.81.168.237");
try {
//get a handle to the URLConnection object for this url
URLConnection uc = openConnection();
System.out.println("Error message " + uc.getRequestCode());
//Check whether this URL object can be "read"
if (uc.getDoInput() == true) {
//Attach an input Stream and read from this input stream
InputStream iStream = InputStream();
DataInputStream data = new iStream);
System.out.println("URL is valid");
data.close(); //close the input stream

}
} catch (IOException ie) {
System.out.println("URL is NOT valid.");
// retCode = false;
}
}catch (MalformedURLException e) {
System.out.println("Caught MUE !");
// retCode = false;
}
// return retCode;
}
}
Kevin Li
Greenhorn

Joined: Sep 25, 2001
Posts: 21
Here is a example for open url and read stream.
public static String readURL(String urlstr){
StringBuffer sb = new StringBuffer();
try{
URL url = new URL(urlstr);
BufferedReader in = new BufferedReader(
new InputStreamReader(
url.openConnection().getInputStream()));
int tmp = -1;
char temp = ' ';
while((tmp=in.read())!=-1){
temp = (char)tmp;
sb = sb.append(temp);
}
in.close();
}catch(IOException ioe){
ioe.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
return sb.toString();
}
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
"srod"
Please review the JavaRanch Naming Policy then edit your profile so that your publicly displayed name complies with the rules.
Thanks for your cooperation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using the openConnect()
 
Similar Threads
Reading web page from servlet
Downloading a file
Sending mail
Multithreading client socket throws Stream corrupted exception
I am able to display the file information, but I am having problem displaying the employee details.