2014-02-27 36 views
-3

我可以使用此代碼來獲取任何網站,我想的來源,除了這個特定的網站:http://www.minecraftforum.net/topic/1868015-are-these-some-good-specs-for-a-server/試圖讓這個特定頁面的源代碼中的Java

下面是代碼:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.URL; 


public class Main { 

    public static void main(String[] args) throws UnsupportedEncodingException, IOException{ 
     String sURL = "http://minecraftforum.net/topic/2237209-free-kit-god%E2%97%84netrocraft%E2%96%BA-factions-247-auctions-lotto/page__st__320"; 
     System.out.println(sURL); 
     URL url = new URL(sURL); 
     HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); 
     //set http request headers 
     httpCon.addRequestProperty("Host", "www.cumhuriyet.com.tr"); 
     httpCon.addRequestProperty("Connection", "keep-alive"); 
     httpCon.addRequestProperty("Cache-Control", "max-age=0"); 
     httpCon.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); 
     httpCon.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"); 
     httpCon.addRequestProperty("Accept-Encoding", "gzip,deflate,sdch"); 
     httpCon.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); 
     //httpCon.addRequestProperty("Cookie", "JSESSIONID=EC0F373FCC023CD3B8B9C1E2E2F7606C; lang=tr; __utma=169322547.1217782332.1386173665.1386173665.1386173665.1; __utmb=169322547.1.10.1386173665; __utmc=169322547; __utmz=169322547.1386173665.1.1.utmcsr=stackoverflow.com|utmccn=(referral)|utmcmd=referral|utmcct=/questions/8616781/how-to-get-a-web-pages-source-code-from-java; __gads=ID=3ab4e50d8713e391:T=1386173664:S=ALNI_Mb8N_wW0xS_wRa68vhR0gTRl8MwFA; scrElm=body"); 
     HttpURLConnection.setFollowRedirects(false); 
     httpCon.setInstanceFollowRedirects(false); 
     httpCon.setDoOutput(true); 
     httpCon.setUseCaches(true); 

     httpCon.setRequestMethod("GET"); 

     BufferedReader in = new BufferedReader(new `InputStreamReader(httpCon.getInputStream(), "UTF-8"));` 
     String inputLine; 
     StringBuilder a = new StringBuilder(); 
     while ((inputLine = in.readLine()) != null) 
      a.append(inputLine); 
     in.close(); 

     System.out.println(a.toString()); 

     httpCon.disconnect(); 

    } 

}

+1

那麼它會返回一個異常或沒有源?或者說,該頁面的來源非常簡短? – xlm

回答

1

我想你從另一個問題得到這段代碼? How to get a web page's source code from Java

但是你的問題很可能是: httpCon.addRequestProperty("Host", "www.cumhuriyet.com.tr");

您需要更改主機URL到: httpCon.addRequestProperty("Host", "www.minecraftforum.net");

接受這個答案,關閉問題,如果它的工作。謝謝。

相關問題