2

目前我正在尝试编写一个程序,它会返回一个列表,其中包含 booklooker.de 上书籍列表中最低价格的列表。该网站本身使用https,我想这就是问题所在。

代码:

import java.io.*;
import java.net.*;
import javax.net.ssl.HttpsURLConnection;

public class Booklooker {
    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub
        BufferedReader in = new BufferedReader(new FileReader("list.txt"));
        PrintStream out = new PrintStream(new FileOutputStream(new File("result.txt")));

        String book = "";
        while((book = in.readLine()) != null){
            book.replace(' ', '+').replace("ä", "%E4").replace("ö", "%F6").replace("ü", "%FC").replace("ß", "%DF");
            URL link = new URL("https://secure.booklooker.de/app/result.php?sortOrder=preis_total&setMediaType=0&titel="+book);
            HttpsURLConnection con = (HttpsURLConnection)link.openConnection();
            BufferedReader site = new BufferedReader(new InputStreamReader(con.getInputStream()));
            while(true){
                String line = "";
                if((line = site.readLine()) == null){
                    out.print("Error\n");
                    break;
                }
                else if(line.indexOf(" €") != -1){
                    int    index   = line.indexOf(" €");
                    double price   = Double.parseDouble(line.substring(index-5, index).trim().replace(',', '.'));
                    index          = line.indexOf(" €", index+12);
                    double postage = Double.parseDouble(line.substring(index-5, index).trim().replace(',', '.'));
                    out.print(price+postage+"\n");
                    break;
                }
            }
            site.close();
        }
        in.close();
        out.close();
        System.out.println("Finished.");
    }
}

并返回错误:

Exception in thread "main" java.io.IOException: Invalid Http response
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at Booklooker.main(Booklooker.java:16)

我不知道到底出了什么问题,但 HttpsURLConnection 似乎有问题。有人有想法吗?

4

2 回答 2

0

您的网址无效。删除尾随 & 号。这里没有 HTTPS 或 SSL 问题,否则您会遇到不同的异常。

于 2013-08-18T10:34:04.500 回答
0

您是否尝试过使用https://www.booklooker.de/(而不是安全的.booklooker.de)?使用“安全”时,您将获得可能是问题的重定向。

于 2018-09-22T11:48:09.960 回答