打开www.yahoo.com.cn 怎么会  con.getResponseCode() 不等于 HttpURLConnection.HTTP_OK 

解决方案 »

  1.   

    我的代码是:import java.io.*;
    import java.net.*;
    public class webgethead { /**
     * @param args
     */
    public static void main(String s[]) {
        System.out.println(exists("http://www.sina.com.cn"));
        System.out.println(exists("http://localhost/1.asp"));
        System.out.println(exists("http://www.yahoo.com.cn"));
       } static boolean exists(String URLName){
      try {
        HttpURLConnection.setFollowRedirects(false);
        HttpURLConnection con =
           (HttpURLConnection) new URL(URLName).openConnection();
        //con.setRequestMethod ("HEAD");
        //con.setFollowRedirects(true);  //即使加上也没有用
         if (con.getResponseCode() == HttpURLConnection.HTTP_OK){
       System.out.println();
        System.out.print( "getHeaderFields:"  + con.getHeaderField("X-Cache"));
        System.out.println();
         return true;
      }else{
       return false;
       }
     
        }
      catch (Exception e) {
           e.printStackTrace();
           return false;
           }
      }}