简体   繁体   中英

Java - Unable to obtain HTML plaintext from webside

I have a strange problem. I have in the past used a program I wrote myself to check if a new chapter has come out on a story at fanfiction.net and that program works fine even now (though its GUI leaves a lot to wish for).

However, when I am trying to make a new version I can't seem to load the webpage even though I'm using the exact same code (Copy Pasted). This is the code below. When sending in a URL like https://www.fanfiction.net/s/11012678/36 to the nextExists method it should return 'true'. My old program does, but this one doesn't even though it's the same code.

The only thing I can think of that might have any effect would be that I am using a new version of Eclipse which might cause it to mistake the Encoding, but I have tried checking all the common encoding types and nothing provides the HTML plaintext.

Does anyone have any idea what might be causing this? It's not a disaster if I can't get this right but I would like to know for the future in case I run into the same problem again.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class Util {
    private static final String BEFORE = "<button class=btn TYPE=BUTTON onClick=\"self.location='", AFTER = "'\">Next &gt;</button>", SITE = "fanfiction.net";

    public static String readSite(String path) throws Exception{
        URL url = new URL(path);
        BufferedReader in = null;
        String line;
        try{
            StringBuilder builder = new StringBuilder();
            in = new BufferedReader(new InputStreamReader(url.openStream()));
            line = in.readLine();
            if(line == null){
                return null;
            }
            builder.append(line);
            while((line = in.readLine()) != null){
                builder.append('\n' + line);
            }
            return builder.toString();
        } finally{
            if(in != null){
                in.close();
            }
        }
    }

    public static String updatePathToEnd(String path) throws Exception{
        outer: while(nextExists(path)){
            String data = readSite(path);
            if(path.contains(SITE)){
                String link = path.substring(0, path.indexOf(SITE) + SITE.length()) + data.substring(data.indexOf(BEFORE) + BEFORE.length(), data.indexOf(AFTER));
                if(readSite(link) != null) {
                    path = link;
                    continue outer;
                }
            }
        }
        return path;
    }

    public static boolean nextExists(String path) throws Exception{
        String text = readSite(path);
        if(path.contains(SITE)){
            return text==null ? false : text.contains(AFTER);
        }
        return false;
    }

}

我在bluej中尝试过并且工作完美,看来问题出在Eciplse Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM