Question

Hi I am using a facebook posts feeding application which will read all the new posts from my facebook page and will save to my sharepoint list. Earlier, i.e, before June it has worked properly it feeds all the posts from fabebook to my Share point list. But nowadays its throws an error while getting response from the Facebook authorize url. I don't know what went wrong. Please help me if you guys have any suggestion to resolve this issue. I am appending my code part below.

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(); 

The above line throws WebExceptopn which is teeling like Process has timed out.

            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);
        }
    }
Était-ce utile?

La solution

Hi Finally i got the solution.

Here i am using getResponse = (HttpWebResponse)getRequest.GetResponse();

The problem is we can use only one HttpWebResponse at a time. In my case i am using the same object in two times without any disposing and closing. That's make me the error.

So I updated my code like this.

 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();

This solved my error. Thanks

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top