Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using the openConnect()

 
von rodrig
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic