quarta-feira, 29 de julho de 2015

Facebook Request C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Collections.Specialized;
using System.Net;
using System.IO;

namespace Lapuinka.Facebook.Util
{
    public class FacebookResponse
    {
        public enum Method { GET, POST };
        public const string Authorize = "https://graph.facebook.com/oauth/authorize";
        public const string Access_Token = "https://graph.facebook.com/oauth/access_token";
        public string CallBack_Url = "";

        private string _appFacebookID = "";
        public string AppFacebookID
        {
            get
            {
                _appFacebookID = ConfigurationManager.AppSettings["appFacebookID"];
                return _appFacebookID;
            }
        }

        private string _appFacebookSecret = "";
        public string AppFacebookSecret
        {
            get
            {
                _appFacebookSecret = ConfigurationManager.AppSettings["appFacebookSecret"];
                return _appFacebookSecret;
            }
        }

        private string _token = "";
        public string Token
        {
            get { return _token; }
            set { _token = value; }
        }

        public void GetAccessToken(string accessCode)
        {
            string accessTokenUrl = string.Format("{0}?client_id={1}&redirect_uri={2}&client_secret={3}&code={4}",
            Access_Token, this.AppFacebookID, CallBack_Url, this.AppFacebookSecret, accessCode);

            string response = RequestFace(Method.GET, accessTokenUrl, String.Empty);

            if (response.Length > 0)
            {
                NameValueCollection qs = HttpUtility.ParseQueryString(response);

                if (qs["access_token"] != null)
                {
                    this.Token = qs["access_token"];
                }
            }
        }

        public string RequestFace(Method method, string url, string postData)
        {
            HttpWebRequest webRequest = null;
            StreamWriter requestWriter = null;
            string responseData = "";

            webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
            webRequest.Method = method.ToString();
            webRequest.ServicePoint.Expect100Continue = false;
            //webRequest.UserAgent = "[Seu user agent]";
            webRequest.Timeout = 20000;

            if (method == Method.POST)
            {
                webRequest.ContentType = "application/x-www-form-urlencoded";

                //POST the data.
                requestWriter = new StreamWriter(webRequest.GetRequestStream());

                try
                {
                    requestWriter.Write(postData);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    requestWriter.Close();
                    requestWriter = null;
                }
            }

            responseData = GetWebResponse(webRequest);
            webRequest = null;
            return responseData;
        }

        public string GetWebResponse(HttpWebRequest webRequest)
        {
            StreamReader responseReader = null;
            string responseData = "";

            try
            {
                responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream());
                responseData = responseReader.ReadToEnd();
            }
            catch
            {
                throw;
            }
            finally
            {
                webRequest.GetResponse().GetResponseStream().Close();
                responseReader.Close();
                responseReader = null;
            }

            return responseData;
        }
    }

}
.
.
.
More Utils

    public static string GetWebResponse(string url)
        {
            string responseString = string.Empty;
            WebRequest request = WebRequest.Create(url);
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            using (StreamReader reader = new StreamReader(request.GetResponse().GetResponseStream()))
            {
                responseString = reader.ReadToEnd();
            }
            return responseString;
        }

        public static string HttpGet(string URI)
        {
            WebRequest req = WebRequest.Create(URI);
            string ProxyString = "";
            req.Proxy = new System.Net.WebProxy(ProxyString, true); //true means no proxy
            System.Net.WebResponse resp = req.GetResponse();
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }

        public static string HttpPost(string URI, string Parameters)
        {
            System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
            req.Proxy = new System.Net.WebProxy(ProxyString, true);
            //Add these, as we're doing a POST
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";
            //We need to count how many bytes we're sending. Post'ed Faked Forms should be name=value&
            byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
            req.ContentLength = bytes.Length;
            System.IO.Stream os = req.GetRequestStream();
            os.Write(bytes, 0, bytes.Length); //Push it out there
            os.Close();
            System.Net.WebResponse resp = req.GetResponse();
            if (resp == null) return null;
            System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }


Postar um comentário