[問題] URL類別連線網址 只有無名失敗@@?

看板java作者 (小董)時間17年前 (2009/02/18 16:24), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串1/5 (看更多)
我用URL類別去建立連線(程式附在最後) 但是某些網站就失效了 像是我要連線到無名的任何一個頁面 包括首頁,就出現了以下的錯誤訊息 C:\>java n 主網頁的大小為 -1, 類型為 text/html; charset=utf-8 發生了 java.io.IOException: Server returned HTTP response code: 403 for URL: htt p://www.wretch.cc/ 例外 C:\>java n 主網頁的大小為 -1, 類型為 text/html; charset=utf-8 發生了 java.io.IOException: Server returned HTTP response code: 403 for URL: htt p://www.wretch.cc/blog/xxxxxx 例外 如果是連線到GOOGLE或是YAHOO的話就可以 GOOGLE(http://www.google.com.tw) C:\>java n 主網頁的大小為 -1, 類型為 text/html; charset=UTF-8 <html><head><meta http-equiv="content-type" content="text/html; charset=Big5"><t itle>Google</title><script>var _gjwl=location;function _gjuc(){var a=_gjwl.hash; if(a.indexOf("&q=")>0||a.indexOf("#q=")>=0){a=a.substring(1);if(a.indexOf("#")== ... ...原始碼.... YAHOO(http://tw.yahoo.com) C:\>java n 主網頁的大小為 -1, 類型為 text/html; charset=utf-8 <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/tr/html4/st rict.dtd'><html lang="zh-tw"><head><meta http-equiv="Content-Type" content="text /html; charset=utf-8"><meta http-equiv="expires" content="-1"><base href=http:// ... ...原始碼.... 甚至連這種http://www.javaworld.com.tw/jute/post/view?bid=5&id=139293&sty=3 的複雜(非普通靜態網頁)網址都能連線成功 並且下載到原始碼 不知道這是不是某種 Sever 檢測出問題?不然怎麼獨有無名不行連結@@ 例如說像是相簿圖片就會有來源檢測(Referer) 或是說必須為裝成IE firefox等等? 我剛剛在網路上爬了一個下午的文章 好像有種說法是要用到 socket 去使用 http 協定去連線? 不過這種要怎麼做..?還是說那裡可以查到資料 我找了一個下午都找不到...還是我關鍵字 下錯了@@? 不知道URL類別能不能直接解決這樣的問題 而不需要動用到socket?? 希望眾高手指點指點啊~~ 附件:Source Code import java.io.*; import java.net.*; import java.util.*; public class n { public static void main(String args[]) { String str; try { URL page = new URL("http://www.wretch.cc/"); URLConnection page_C = page.openConnection(); page_C.setRequestProperty("User-Agent", "Mozilla/4.0 (co Windows XP; SV1; .NET CLR 1.1.4322)"); System.out.print("主網頁的大小為 " + page_C.getContentLe System.out.println(", 類型為 " + page_C.getContentType() Object obj = page.getContent(); InputStreamReader isr = new InputStreamReader((InputStre BufferedReader br = new BufferedReader(isr); while((str = br.readLine())!=null) { System.out.print(str); } br.close(); } catch(IOException ioe) { System.out.print("發生了 " + ioe + " 例外\n"); } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 114.42.212.210 ※ 編輯: rps12369 來自: 114.42.212.210 (02/18 16:25)

02/18 16:44, , 1F
User-Agent, referer
02/18 16:44, 1F
老實說我對 setRequestProperty 這個部分不是很了解... 不知道有沒有人告訴我該怎麼做 還有哪裡可以查到這類的東西的資料? 這是我更改後的碼 page_C.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)"); page_C.setRequestProperty("referer", "http://tw.yahoo.com"); 一樣是有錯誤訊息 而這是我用截取http header的程式所抓到的資料 不知道有沒有助於我上面程式的幫忙 C:\>java HTTPHeaders http://www.wretch.cc Request properties.... User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322 ) Referer: http://www.wretch.cc Response properties.... Date: Wed, 18 Feb 2009 09:31:41 GMT Set-Cookie: BX=32i5b594pnlbt&b=3&s=oo; expires=Tue, 02-Jun-2037 20:00:00 GMT; pa th=/; domain=.wretch.cc P3P: policyref="http://p3p.yahoo.com/w3c/p3p.xml", CP="CAO DSP COR CUR ADM DEV T AI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE GOV" Set-Cookie: PHPSESSID=j6r7g7m72s8gqfkhrjcoc9rf36; path=/; domain=.wretch.cc Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Set-Cookie: lang=deleted; expires=Tue, 19-Feb-2008 09:31:40 GMT; path=/; domain= www.wretch.cc Set-Cookie: lang=en; expires=Sat, 13-Feb-2010 09:31:41 GMT; path=/; domain=.wret ch.cc Vary: Accept-Encoding Content-Type: text/html; charset=utf-8 Age: 0 Transfer-Encoding: chunked Connection: keep-alive Via: HTTP/1.1 r4.ycpi.tp2.yahoo.net (YahooTrafficServer/1.17.9 [c sSf ]) Server: YTS/1.17.9 Hit enter to continue ※ 編輯: rps12369 來自: 114.42.212.210 (02/18 17:34)
文章代碼(AID): #19cyMhk6 (java)
討論串 (同標題文章)
文章代碼(AID): #19cyMhk6 (java)