JSoup UserAgent,如何正确设置? [英] JSoup UserAgent, how to set it right?
问题描述
我正在尝试使用JSoup
解析facebook的首页,但是我总是获得移动设备的HTML代码,而不是常规浏览器的版本(在我的情况下是Firefox 5.0).
I'm trying to parse the frontpage of facebook with JSoup
but I always get the HTML Code for mobile devices and not the version for normal browsers(In my case Firefox 5.0).
我正在这样设置我的用户代理:
I'm setting my User Agent like this:
doc = Jsoup.connect(url)
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0")
.get();
我做错什么了吗?
我刚刚解析了 http://whatsmyuseragent.com/,并且该用户代理正在运行.现在,令我更加困惑的是,为什么在使用JSoup和我的网站时,网站 http://www.facebook.com/返回不同的版本的原因浏览器.两者都使用相同的useragent....
I just parsed http://whatsmyuseragent.com/ and it looks like the user Agent is working. Now its even more confusing for me why the site http://www.facebook.com/ returns a different version when using JSoup and my browser. Both are using the same useragent....
我现在也在其他一些站点上也注意到了这种行为.如果您能向我解释什么是问题,我会很高兴的.
I noticed this behaviour on some other sites too now. If you could explain to me what the Issue is I would be more than happy.
推荐答案
您也可以尝试设置引荐来源标头:
You might try setting the referrer header as well:
doc = Jsoup.connect("https://www.facebook.com/")
.userAgent("Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6")
.referrer("http://www.google.com")
.get();
这篇关于JSoup UserAgent,如何正确设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!