【问题标题】:Reading from an https-URL in java从 Java 中的 https-URL 读取
【发布时间】:2013-08-20 07:44:17
【问题描述】:

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

【问题讨论】:

    标签: java ssl https inputstream httpsurlconnection


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 不,URL 是有效的,有和没有尾随 & 号。这种情况下,为什么报错说Invalid Http response at HttpsURLConnectionImpl.getInputStream(Unknown Source)?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 2017-11-10
      • 2019-06-21
      • 1970-01-01
      • 2011-07-19
      相关资源
      最近更新 更多