2013-08-18 32 views
2

当前我正在尝试编写一个程序,该程序返回booklooker.de上书籍列表的最低价格列表。该网站本身使用https,我想,这就是问题所在。从java中的https-URL读取

代码:

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的一个问题。有人有想法吗?

回答

0

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

+0

不,网址是有效的,有和没有尾随&号。在这种情况下,为什么错误报告会在HttpsURLConnectionImpl.getInputStream(Unknown Source)中声明无效的Http响应? – njcasdewdwas