从java中的https-URL读取

时间:2013-08-18 10:23:02

标签: java ssl https inputstream httpsurlconnection

目前我正在尝试编写一个程序,它会在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似乎有问题。有人有想法吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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