日期:2014-05-20  浏览次数:20859 次

Server returned HTTP response code: 403 for URL怎么解决
Server returned HTTP response code: 403 for URL: http://f.youku.com/player/getFlvPath/sid/00_00/st/flv/fileid/03000201004ED6BF48F027006A8E437E675E32-B23A-7BF2-3B22-0332C714DFDB?K=8059f0d62a4e740f261c083f

网上找了很多方法,都不能搞定

下面是网上找到一个方法:
最近在做一个基于不同协议实现跨库资源检索的小程序,在基于OpenURL时,采用网络抓取的形式获取结果页,其中遇到这样的错误:
Server returned HTTP response code: 403 for URL: http://www……………………而可以使用浏览器正确访问
 
 因为服务器的安全设置不接受Java程序作为客户端访问

解决方案:设置User Agent

即在url.openConnection()后添加:

connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");


但是我就是不行,换成了Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)也不成,是不是和系统和IE版本有关。


------解决方案--------------------
你用httpwatch看看,其实发了2个请求,第一组:

请求:
Java code

GET /player/getFlvPath/sid/00_00/st/flv/fileid/03000201004ED6BF48F027006A8E437E675E32-B23A-7BF2-3B22-0332C714DFDB?K=8059f0d62a4e740f261c083f HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/QVOD, application/QVOD, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322; VENUS_IE_ADDON-0.2.7.95)
Accept-Encoding: gzip, deflate
Host: f.youku.com
Connection: Keep-Alive

------解决方案--------------------
第二个请求:

GET /youku/656ECACC36357C75F7394338/03000201004ED6BF48F027006A8E437E675E32-B23A-7BF2-3B22-0332C714DFDB.flv HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/QVOD, application/QVOD, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET CLR 1.1.4322; VENUS_IE_ADDON-0.2.7.95)
Accept-Encoding: gzip, deflate
Host: 61.55.189.49
Connection: Keep-Alive

返回

HTTP/1.1 200 OK
Content-Type: video/x-flv
Accept-Ranges: bytes
ETag: "4140106537"
Last-Modified: Thu, 01 Dec 2011 07:44:16 GMT
Content-Length: 3053501
Connection: close
Date: Thu, 01 Dec 2011 09:19:59 GMT
Server: YOUKU.SJZ

<body部分省略>