【发布时间】:2015-05-21 07:21:19
【问题描述】:
我想检查一个 facebook 用户是否喜欢我的 facebook 页面。我有很多使用 javascript 的解决方案,但我想在 ASP.Net 中实现这个要求。
我从以下链接复制了代码: http://duanedawnrae.com/Blog/post/2012/02/29/Determine-if-a-Facebook-user-Likes-your-page-with-ASPNET.aspx
我得到了下面的 ASP.Net 代码,它的工作原理相同。
ASP.Net 代码:
public class WebService : System.Web.Services.WebService
{
[WebMethod()]
public string GetFacebookLikeStatus(string fbpageid, string fbappid, string fbtoken, string fburl)
{
string strReturn = null;
// Placeholder for the Facbook "like" API call
string strURL = null;
strURL = "https://graph.facebook.com/me/likes?access_token=" + fbtoken;
// Placeholder for the Facebook GET response
WebRequest objGETURL = null;
objGETURL = WebRequest.Create(strURL);
// Declare response stream
Stream objStream = null;
// Declare The Facebook response
string strLine = null;
// Declare a count on the search term
int intStr = 0;
try
{
// Create an instance of the StreamReader
StreamReader objReader = new StreamReader(objStream);
// Get the response from the Facebook API as a JSON string.
// If access_token is not correct for the logged
// on user Facebook returns (400) bad request error
objStream = objGETURL.GetResponse().GetResponseStream();
// If all is well
try
{
// Execute the StreamReader
strLine = objReader.ReadToEnd().ToString();
// Check if Facebook page Id exists or not
intStr = strLine.IndexOf(fbpageid); // if valid return a value
if (intStr > 0)
{
strReturn = "1";
// if not valid return a value
}
else
{
strReturn = "0";
}
objStream.Dispose();
}
catch (Exception ex)
{
// For testing comment out for production
strReturn = ex.ToString();
// Uncomment below for production
//strReturn = "Some friendly error message"
}
}
catch (Exception ex)
{
// For testing comment out for production
strReturn = ex.ToString();
// Uncomment below for production
//strReturn = "Some friendly error message"
}
return strReturn;
}
}
以上代码包含一个包含单个函数的 web 服务。该函数包含四个输入参数并返回一个输出字符串。
但是当我运行这个网络服务时,我得到了错误,“值不能为空。参数名称:流”。出现此错误是因为“objStream”变量设置为空。请解决此问题,以便我可以得到正确的输出,因为我不知道如何实现我的要求。
【问题讨论】:
标签: c# asp.net facebook facebook-graph-api