我想在我的网站上使用 facebook like 按钮。由于我的大多数访问者来自伊朗,而 facebook 在伊朗被过滤,如果有人来自伊朗,喜欢按钮会被过滤,页面会变得非常难看。我想防止这种情况是使用 webclient 尝试连接到 facebook。如果成功,我会放置like按钮,否则我不会:
string fb_result="failed";
WebClient webclient= new WebClient();
try{
fb_result=webclient.DownloadString("http://www.fabebook.com");
}
catch{
fb_result="failed"; //if its being filtered exception is raised
}
在我的 html 中:
<%if(fb_result)!="failed"){%>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-like" data-href="http://www.mysite.com" data-send="true" data-width="450" data-show-faces="true"></div>
<%}%>
这工作正常,但问题是,当 webclient 无法连接时,引发错误需要很长时间。有什么方法可以让 webclient 尝试更短的时间,如果无法连接更快地引发错误。
顺便说一句,检查是否可以连接到 facebook 的任何其他方法都适用。因为这可能不是检查它的唯一方法。