6

我在 Bean 中使用以下 Java 代码来读取 URL 的内容:

String url;
String inputLine;
StringBuilder srcCode=new StringBuilder();

public void setUrl (String value) {
    url = value; 
}

private void scanWebPage() throws IOException {
    try {
         URL dest = new URL(url);
         URLConnection yc =  dest.openConnection();
         yc.setUseCaches(false);
         BufferedReader in = new BufferedReader(new 
                        InputStreamReader(yc.getInputStream()));
         while ((inputLine = in.readLine()) != null)
            srcCode = srcCode.append (inputLine);
         in.close();
    } catch (FileNotFoundException fne) {
         srcCode.append("File Not Found") ;
    }
}

该代码适用于大多数 URL,但不适用于重定向的 URL。如何更新上述代码以从重定向的 URL 中读取内容?对于重定向的 URL,我得到"File Not Found".

4

2 回答 2

4

试一试:

    HttpURLConnection yc =  (HttpURLConnection) dest.openConnection();
    yc.setInstanceFollowRedirects( true );

在您上面的代码的上下文中:

   `String url = "http://java.sun.com";
    String inputLine;
    StringBuilder srcCode=new StringBuilder();



    URL dest = new URL(url);
    HttpURLConnection yc =  (HttpURLConnection) dest.openConnection();
    yc.setInstanceFollowRedirects( true );
    yc.setUseCaches(false);

    BufferedReader in = new BufferedReader(
        new InputStreamReader(
            yc.getInputStream()));
    while ((inputLine = in.readLine()) != null) {
        srcCode = srcCode.append (inputLine);
    }

    in.close();`

进一步修改以帮助您诊断正在发生的事情。此代码关闭自动重定向,然后手动跟随位置标头打印出来。

@Test
public void f() throws IOException {
    String url = "http://java.sun.com";


    fetchURL(url);
}


private HttpURLConnection fetchURL( String url ) throws IOException {
    URL dest = new URL(url);
    HttpURLConnection yc =  (HttpURLConnection) dest.openConnection();
    yc.setInstanceFollowRedirects( false );
    yc.setUseCaches(false);

    System.out.println( "url = " + url );

    int responseCode = yc.getResponseCode();
    if ( responseCode >= 300 && responseCode < 400 ) { // brute force check, far too wide
        return fetchURL( yc.getHeaderField( "Location") );
    }

    System.out.println( "yc.getResponseCode() = " + yc.getResponseCode() );

    return yc;
}
于 2013-02-27T11:39:49.923 回答
0

它不是你的 prog 的调试,但你可以考虑这个

public class GetURLData
{
    public static void main(String args[])
    {
        String url = "the url you want the response from";
        HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse response;
            StringBuilder builder= new StringBuilder();
            try 
            {
                response = httpClient.execute(httpPost);
                BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                char[] buf = new char[8000];
                int l = 0;
                    while (l >= 0) 
                    {
                        builder.append(buf, 0, l);
                        l = in.read(buf);
                    }
                System.out.println(builder.toString);
            } catch (Exception e) 
        {
                System.out.println("Exception is :"+e);
                e.printStackTrace();
            }
    }
}
于 2013-02-27T12:09:49.990 回答