java - 从java中的https-URL读取

标签 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 似乎有问题。有人有想法吗?

最佳答案

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

关于java - 从java中的https-URL读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18298088/

相关文章:

django - Apache mod_wsgi 错误 : Forbidden

机器人 : mail send using javamail smtp tls is not getting saved in sent folder of icloud account

java - 使用 RC4 算法逐行加密后解密仅产生一行正确的行

java - 如何在 iText 生成的 PDF 中向我的页眉添加图像?

apache - 在 Apache 反向代理后面使用 SSL 的 JIRA 服务器无法正常工作

Intranet Web 应用程序的 http 或 https 身份验证

ssl - WinRm - 由于 SSL 证书不正确,无法在 HTTPS 上创建 WinRM 监听器

http - 尝试使用协议(protocol)分析器 (Charles) 捕获 node.js http 流量,但无法让 Node 使用代理

java - NG 和 JUnit 框架的区别??哪个最适合使用 Selenium Web 驱动程序进行测试

java - 在 Java 中使用 trim() 删除部分输出