作者rps12369 (小董)
看板java
标题[问题] URL类别连线网址 只有无名失败@@?
时间Wed Feb 18 16:24:08 2009
我用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)
1F:推 qrtt1:User-Agent, referer 02/18 16:44
老实说我对 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)