无法访问网站重定向

时间:2018-04-19 11:01:09

标签: java

我有这个链接: http://www.kanas.cz/stranka/jidelna

我需要通过Java访问源代码。 但是,当我访问该网站时,我得到了捷克语的内容。有一个按钮可以将语言更改为英语,但是使用按钮中的链接会给我一个错误。

有什么方法可以访问该网站的英文版本吗?

由于

我的代码:

    String line = null;
    String kanEngPageHtml = null;

    String kanEngLink = "http://www.kanas.cz/stranka/jidelna";
    URL kanEngUrl = new URL(kanEngLink);
    URLConnection kanEngCon = kanEngUrl.openConnection();
    InputStream kanEngIs = kanEngCon.getInputStream();
    BufferedReader kanEngBr = new BufferedReader(new InputStreamReader(kanEngIs,"UTF-8"));

    while ((line = kanEngBr.readLine()) != null) {
        kanEngPageHtml = kanEngPageHtml + "\n" + line;
    }

    System.out.println(kanEngPageHtml);

好的,所以我似乎已经弄明白了。关键是来自VBA的旧宏,我首先将GET请求调用http://www.kanas.cz/language/setlanguage/2#menu然后调用http://www.kanas.cz/stranka/jidelna。所以我想我也可以尝试一下。

关键是从第一个连接中取出烹饪并将它们放在第二个连接中。

public static void main(String[] args) throws IOException {
    String kanEngLink;
    URL kanEngUrl;
    URLConnection kanEngCon;
    String cookiesHeader;
    InputStream kanEngIs;
    BufferedReader kanEngBr;
    String line;
    String kanEngPageHtml="";

    kanEngLink = "http://www.kanas.cz/language/setlanguage/2#menu";
    kanEngUrl = new URL(kanEngLink);
    kanEngCon = (HttpURLConnection) kanEngUrl.openConnection();
    kanEngCon.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
    cookiesHeader = kanEngCon.getHeaderField("Set-Cookie");

    kanEngLink="http://www.kanas.cz/stranka/jidelna";
    kanEngUrl = new URL(kanEngLink);
    kanEngCon = (HttpURLConnection) kanEngUrl.openConnection();
    kanEngCon.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
    kanEngCon.setRequestProperty("Cookie", cookiesHeader);

    kanEngIs = kanEngCon.getInputStream();
    kanEngBr = new BufferedReader(new InputStreamReader(kanEngIs,"UTF-8"));

    while ((line = kanEngBr.readLine()) != null) {
        kanEngPageHtml = kanEngPageHtml + "\n" + line;
    }

    System.out.println(kanEngPageHtml);
}

1 个答案:

答案 0 :(得分:0)

关键是从第一个连接中取出烹饪并将它们放在第二个连接中。

public static void main(String[] args) throws IOException {
    String kanEngLink;
    URL kanEngUrl;
    URLConnection kanEngCon;
    String cookiesHeader;
    InputStream kanEngIs;
    BufferedReader kanEngBr;
    String line;
    String kanEngPageHtml="";

    kanEngLink = "http://www.kanas.cz/language/setlanguage/2#menu";
    kanEngUrl = new URL(kanEngLink);
    kanEngCon = (HttpURLConnection) kanEngUrl.openConnection();
    kanEngCon.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
    cookiesHeader = kanEngCon.getHeaderField("Set-Cookie");

    kanEngLink="http://www.kanas.cz/stranka/jidelna";
    kanEngUrl = new URL(kanEngLink);
    kanEngCon = (HttpURLConnection) kanEngUrl.openConnection();
    kanEngCon.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB;     rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 (.NET CLR 3.5.30729)");
    kanEngCon.setRequestProperty("Cookie", cookiesHeader);

    kanEngIs = kanEngCon.getInputStream();
    kanEngBr = new BufferedReader(new InputStreamReader(kanEngIs,"UTF-8"));

    while ((line = kanEngBr.readLine()) != null) {
        kanEngPageHtml = kanEngPageHtml + "\n" + line;
    }

    System.out.println(kanEngPageHtml);
}