• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using the openConnect()

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic