抓取防盗链图片显示
?
图(2)采用"WebRequest"的方式
?
核心代码
1.伪装Referer方式
?
view source print?01
MSXML2.XMLHTTPClass oxmlHttp =
new
MSXML2.XMLHTTPClass();
02
?
?03
oxmlHttp.open(
"Get"
, url,
false
,
null
,
null
);
04
oxmlHttp.setRequestHeader(
"Referer"
, url);
05
oxmlHttp.send(
"0"
);
06
?
?07
if
(oxmlHttp.readyState == 4 && oxmlHttp.status == 200)
08
{
09
????
return
(Byte[])oxmlHttp.responseBody;
10
}
2.WebRequest方式 ?
?
view source print?1
WebRequest request = WebRequest.Create(url);
2
WebResponse response = request.GetResponse();
3
Stream reader = response.GetResponseStream();
3.Iframe方式 ?
?
view source print?1
<
script
>var im = "<
img
src
=
"图片URL"
/>";</
script
>
2
?
?3
<
iframe
id
=
"im"
style
=
"border: 0px; overflow: hidden;"
scrolling
=
"no"
frameborder
=
"0"
src
=
"javascript:parent.im;"
onload
=
"javascript:var x=document.getElementById('im').contentWindow.document.images[0];this.width=x.width+10;this.height=x.height+10;"
></
iframe
>
?
?
?
总结
一开始使用的是WebRequest的方式,将获取到的内容写入到MemoryStream中,再输出到页面,但是速度从上面图(2)可见,而且对于BlogBus会返回505的错误。如果有空会再次研究一下这种方式为什么这么慢,瓶颈在哪儿。
使用XMLHTTPClass的方式,注意要返回的属性是responseBody,常见的抓取文本返回的属性是responseText,因为没有注意这个细节,导致始终无法输出图片,关于该类的说明,参考了博客园“一个呆子”同学的文章。网上有一些ASP和PHP的版本,差不多都是用的这种方式。当然这两种方式的代码不仅仅可以做这些,还可以做更多事情。