繁体   English   中英

从Facebook得到响应正在超时

[英]getting response from Facebook is getting timed out

嗨,我正在使用一个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);
        }
    }

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

在这里我正在使用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();

这解决了我的错误。 谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM