0

嗨,我正在使用一个 facebook 帖子馈送应用程序,该应用程序将从我的 facebook 页面读取所有新帖子并将保存到我的共享点列表中。早些时候,即在 6 月之前,它工作正常,它将所有帖子从 fabebook 提供到我的共享点列表。但现在它在从 Facebook 授权 url 获得响应时抛出错误。我不知道出了什么问题。如果你们有任何解决此问题的建议,请帮助我。我在下面附加了我的代码部分。

private void btnSendToList_Click(object sender, EventArgs e)
    {
        try
        {
            if (ConfigurationSettings.AppSettings["fbClientID"] != null)
                strClientID = ConfigurationSettings.AppSettings["fbClientID"];

            if (ConfigurationSettings.AppSettings["fbRedirectURL"] != null)
                strURL = ConfigurationSettings.AppSettings["fbRedirectURL"];

            if (ConfigurationSettings.AppSettings["fbCltSecret"] != null)
                strCltSecret = ConfigurationSettings.AppSettings["fbCltSecret"];

            if (ConfigurationSettings.AppSettings["fbUserId"] != null)
                strUserId = ConfigurationSettings.AppSettings["fbUserId"];

            if (ConfigurationSettings.AppSettings["SPSiteURL"] != null)
                strSiteURL = ConfigurationSettings.AppSettings["SPSiteURL"];


            CookieCollection cookies = new CookieCollection();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.facebook.com");
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(cookies);
            //Get the response from the server and save the cookies from the first request..
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            cookies = response.Cookies;

            string getUrl = "https://www.facebook.com/login.php?login_attempt=1";
            string postData = String.Format("email={0}&pass={1}", "testuser@gmail.com", "test123$"); // Masking credentials.
            getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
            getRequest.CookieContainer = request.CookieContainer;
            getRequest.CookieContainer.Add(cookies);
            getRequest.Method = WebRequestMethods.Http.Post;
            getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            getRequest.AllowWriteStreamBuffering = true;
            getRequest.ProtocolVersion = HttpVersion.Version11;
            getRequest.AllowAutoRedirect = true;
            getRequest.ContentType = "application/x-www-form-urlencoded";

            byteArray = Encoding.ASCII.GetBytes(postData);
            getRequest.ContentLength = byteArray.Length;
            newStream = getRequest.GetRequestStream(); //open connection
            newStream.Write(byteArray, 0, byteArray.Length); // Send the data.

            getResponse = (HttpWebResponse)getRequest.GetResponse();

            getRequest = (HttpWebRequest)WebRequest.Create(string.Format("https://graph.facebook.com/oauth/authorize?client_id={0}&redirect_uri={1}", strClientID, "http://www.facebook.com/connect/login_success.html"));
            getRequest.Method = WebRequestMethods.Http.Get;
            getRequest.CookieContainer = request.CookieContainer;
            getRequest.CookieContainer.Add(cookies);
            getRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            getRequest.AllowWriteStreamBuffering = true;
            //getRequest.ProtocolVersion = HttpVersion.Version11;
            getRequest.AllowAutoRedirect = true;
            getRequest.ContentType = "application/x-www-form-urlencoded";              

            getResponse = (HttpWebResponse)getRequest.GetResponse(); 

上面的行抛出了 WebExceptopn,它就像 Process 已经超时一样。

            strAccessToken = getResponse.ResponseUri.Query;
            strAccessToken = strAccessToken.Replace("#_=_", "");
            strAccessToken = strAccessToken.Replace("?code=", "");

            newStream.Close();

            if (!string.IsNullOrEmpty(strAccessToken))
                strCode = strAccessToken;

            if (!string.IsNullOrEmpty(strCode) && !string.IsNullOrEmpty(strClientID) &&
                    !string.IsNullOrEmpty(strURL) && !string.IsNullOrEmpty(strCltSecret) &&
                            !string.IsNullOrEmpty(strUserId))
            {
                SaveToList();
            }
        }
        catch (Exception ex)
        {
            LogError(ex);
        }
    }
4

1 回答 1

0

嗨最后我得到了解决方案。

我在这里使用getResponse = (HttpWebResponse)getRequest.GetResponse();

问题是我们一次只能使用一个 HttpWebResponse。就我而言,我两次使用同一个对象而没有任何处置和关闭。这让我犯了错误。

所以我像这样更新了我的代码。

 byteArray = Encoding.ASCII.GetBytes(postData);
        getRequest.ContentLength = byteArray.Length;
        newStream = getRequest.GetRequestStream(); //open connection
        newStream.Write(byteArray, 0, byteArray.Length); // Send the data.

        getResponse = (HttpWebResponse)getRequest.GetResponse();
        getresponse.Close();

这解决了我的错误。谢谢

于 2013-10-11T11:29:25.723 回答