比如 http://www.abc.om 会转到 http://www.efg.com
如何监测到? 需要监测的URL很多,所以需要程序判断,请指点?

解决方案 »

  1.   

    可以用java 的URLConnection自己写一个简单的客户端,然后
    取回文档,分析头部,里面有要重定向的地址
      

  2.   

    好象还是不行呀:
    import java.net.*;
    import java.util.*;
    import java.io.*;public class HttpTest {    public static void getPage(String urlString)  throws IOException {
            // Retrieve the page at `urlString' and print the first 500 bytes.

            URL url = new URL(urlString);
      InputStream pageStream;
    int ch;
    int count = 0;
    HttpURLConnection connection = null;  if(url.getProtocol().equalsIgnoreCase("http")){
      try {
        System.out.println("====  RETRIEVING " + url + "  ====");
        System.out.println();
        connection = (HttpURLConnection) url.openConnection();
        System.err.println("HttpURLConnection opened: " + connection);
        pageStream = connection.getInputStream();
        System.err.println("HttpURLConnection input Stream: " + pageStream);
        System.err.print("Response: ");
        System.err.print(connection.getResponseCode());
        System.err.println(" " + connection.getResponseMessage());
        System.out.println();
        System.out.println("====  CONTENT  ====");
        System.out.println();
        for (ch = pageStream.read() ; ch !=-1 ; ch = pageStream.read()) {
          if (++count < 500) {
    System.out.write(ch);
          } else if (count == 500) {
    System.out.println();
    System.out.println("<More...>");
          }
        }
        System.out.println();
        System.out.println("====  DONE " + count + " bytes  ====");
        System.out.println();
      } catch (Exception e) {
        System.err.println();
        System.err.println("*** ERROR: " + e);
        e.printStackTrace();
        System.err.println();
      }
    }
        }  public static void main(String args[]) {  //1.asp 重定向到2.asp ,可是没有显示有2.asp的地址呀? 请问怎么搞?
        String[] urls
          = new String [] {"http://localhost/1.asp",  "http://localhost/3.html" };    for (int i = 0; i < urls.length; i++) {
          try {
    getPage(urls[i]);
          } catch (Exception e) {
    System.err.println();
    System.err.println("**** Error: " + e);
    e.printStackTrace();
    System.err.println();
          }
        }
      }
    }
    /////////////结果:C:\Program Files\Java\jdk1.5.0_02\bin>java HttpTest
    ====  RETRIEVING http://localhost/1.asp  ====HttpURLConnection opened: sun.net.www.protocol.http.HttpURLConnection:http://loc
    alhost/1.asp
    HttpURLConnection input Stream: sun.net.www.protocol.http.HttpURLConnection$Http
    InputStream@c17164
    Response: 200 OK====  CONTENT  ====this is 2.asp'contect.
    ====  DONE 22 bytes  ========  RETRIEVING http://localhost/3.html  ====HttpURLConnection opened: sun.net.www.protocol.http.HttpURLConnection:http://loc
    alhost/3.html
    HttpURLConnection input Stream: sun.net.www.protocol.http.HttpURLConnection$Http
    InputStream@ca0b6
    Response: 200 OK====  CONTENT  ====3.html
    ====  DONE 6 bytes  ====
      

  3.   

    public static void main(String[] args) {
            HttpClient client;
            client = new HttpClient(new MultiThreadedHttpConnectionManager());
            client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
            client.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");  //让服务器认为是IE        GetMethod get = new GetMethod("http://conut.skycn.com/softdownload.php?id=12665&url=http://hnny.driversky.com/down/absetup1031.exe");
            
            get.setFollowRedirects(false); //禁止自动重定向
            try {
                int iGetResultCode = client.executeMethod(get); 
                System.out.println("====" + get.getResponseHeader("location").getValue()); //打印地址
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                get.releaseConnection();
            }
    }用了 org.apache.commons.httpclient 项目,可以支持所有情况
      

  4.   

    import java.io.ByteArrayInputStream;
    import java.io.IOException;
    import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.HttpClient;
    public class a {
      public static void main(String[] args) {
            HttpClient client;

            client = new HttpClient(new MultiThreadedHttpConnectionManager());
            client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
           // client.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");  //让服务器认为是IE        GetMethod get = new GetMethod("http://conut.skycn.com/softdownload.php?id=12665&url=http://hnny.driversky.com/down/absetup1031.exe");
            
            get.setFollowRedirects(false); //禁止自动重定向
            try {
                int iGetResultCode = client.executeMethod(get); 
                System.out.println("====" + get.getResponseHeader("location").getValue()); //打印地址
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                get.releaseConnection();
            }/**/
       }
    }
    ///////////////////////报下列错误:
    C:\Program Files\Java\jdk1.5.0_02\bin>java a
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lo
    gging/LogFactory
            at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:65)        at a.main(a.java:11)C:\Program Files\Java\jdk1.5.0_02\bin>
      

  5.   

    commons-codec-1.2.jar  commons-logging.jar 
      

  6.   

    返回的code为302才是转发,200是正常返回.
      

  7.   

     public static void main(String args[]) {
     
     HttpClient client = new HttpClient( );
    HttpMethod method = new GetMethod("http://www.yahoo.com.cn");
    String response = null;
    //StringBuffer buffer = new StringBuffer(); try {
        client.executeMethod(method);
        if( method.getStatusCode( ) == HttpStatus.SC_OK ) {
         System.out.println(method.getStatusCode( ));
         System.out.println( method.getResponseHeader("P3P").getValue() );     
        } } catch( HttpException he ) {
    he.printStackTrace();
    } catch( IOException ioe ) {
    ioe.printStackTrace();
    System.out.println(ioe.getMessage());
    } finally {
      method.releaseConnection( );
    }


     }///////////////////////////// http://www.yahoo.com.cn 会转发,实际转发到 http://www.cn.yahoo.com head 信息是 :
    HTTP/1.1 200 OK
    Date: Thu, 03 Nov 2005 04:32:54 GMT
    P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV"
    Connection: close
    Transfer-Encoding: chunked
    Content-Type: text/htmlhead 信息里没有 cn.yahoo.com 呀,怎么得到呢?????