public SocialPostData GetPostData() {
     SocialPostData postData = new SocialPostData();
     if (!String.IsNullOrWhiteSpace(Name)) postData.Add("name", Name);
     if (!String.IsNullOrWhiteSpace(Message)) postData.Add("message", Message);
     if (Privacy != null && Privacy.Value != FacebookPrivacy.Default) postData.Add("privacy", Privacy.ToString());
     return postData;
 }
 /// <summary>
 /// Initializes a new request with default options.
 /// </summary>
 public SocialHttpRequest()
 {
     Method   = SocialHttpMethod.Get;
     Encoding = Encoding.UTF8;
     Timeout  = TimeSpan.FromSeconds(100);
     PostData = new SocialPostData();
 }
 public SocialPostData GetPostData() {
     SocialPostData postData = new SocialPostData();
     if (!String.IsNullOrWhiteSpace(Source)) postData.AddFile("source", Source);
     if (!String.IsNullOrWhiteSpace(Url)) postData.Add("url", Url);
     if (!String.IsNullOrWhiteSpace(Message)) postData.Add("message", Message);
     if (!String.IsNullOrWhiteSpace(Place)) postData.Add("place", Place);
     if (NoStory) postData.Add("no_story", "true");
     return postData;
 }
 public SocialPostData GetPostData() {
     SocialPostData postData = new SocialPostData();
     if (!String.IsNullOrWhiteSpace(Link)) postData.Add("link", Link);
     if (!String.IsNullOrWhiteSpace(Description)) postData.Add("description", Description);
     if (!String.IsNullOrWhiteSpace(Message)) postData.Add("message", Message);
     if (!String.IsNullOrWhiteSpace(Name)) postData.Add("name", Name);
     if (!String.IsNullOrWhiteSpace(Caption)) postData.Add("caption", Caption);
     return postData;
 }
 public SocialPostData GetPostData() {
     SocialPostData data = new SocialPostData();
     data.Add("status", Status ?? "");
     if (ReplyTo != null) data.Add("in_reply_to_status_id", ReplyTo);
     if (PossiblySensitive) data.Add("possibly_sensitive", "true");
     if (Latitude != null) data.Add("lat", Latitude);
     if (Longitude != null) data.Add("long", Longitude);
     if (PlaceId != null) data.Add("place_id", PlaceId);
     if (DisplayCoordinates) data.Add("display_coordinates", "true");
     return data;
 }
        /// <summary>
        /// Makes a POST request to the Facebook API. If the <code>AccessToken</code> property has
        /// been specified, the access token will be appended to the query string.
        /// </summary>
        /// <param name="url">The URL to call.</param>
        /// <param name="query">The query string of the request.</param>
        /// <param name="postData">The POST data.</param>
        /// <param name="isMultipart">If <code>true</code>, the content type of the request will be <code>multipart/form-data</code>, otherwise <code>application/x-www-form-urlencoded</code>.</param>
        /// <returns>Returns an instance of <code>SocialHttpResponse</code> wrapping the response from the Facebook Graph API.</returns>
        public SocialHttpResponse DoAuthenticatedPostRequest(string url, SocialQueryString query, SocialPostData postData, bool isMultipart) {

            // Throw an exception if the URL is empty
            if (String.IsNullOrWhiteSpace(url)) throw new ArgumentNullException("url");

            // Append the HTTP scheme and API version if not already specified.
            if (url.StartsWith("/")) {
                url = "https://graph.facebook.com" + (String.IsNullOrWhiteSpace(Version) ? "" : "/" + Version) + url;
            }

            // Initialize a new instance of SocialQueryString if the one specified is NULL
            if (query == null) query = new SocialQueryString();

            // Append the access token to the query string if present in the client and not already
            // specified in the query string
            if (!query.ContainsKey("access_token") && !String.IsNullOrWhiteSpace(AccessToken)) {
                query.Add("access_token", AccessToken);
            }

            // Append the query string to the URL
            if (!query.IsEmpty) url += (url.Contains("?") ? "&" : "?") + query;

            // Initialize a new HTTP request
            HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);

            // Set the HTTP method
            request.Method = "POST";

            // Write the POST data to the request stream
            if (postData != null && postData.Count > 0) {
                if (isMultipart) {
                    string boundary = Guid.NewGuid().ToString().Replace("-", "");
                    request.ContentType = "multipart/form-data; boundary=" + boundary;
                    using (Stream stream = request.GetRequestStream()) {
                        postData.WriteMultipartFormData(stream, boundary);
                    }
                } else {
                    string dataString = postData.ToString();
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = dataString.Length;
                    using (Stream stream = request.GetRequestStream()) {
                        stream.Write(Encoding.UTF8.GetBytes(dataString), 0, dataString.Length);
                    }
                }
            }

            // Get the HTTP response
            try {
                return SocialHttpResponse.GetFromWebResponse(request.GetResponse() as HttpWebResponse);
            } catch (WebException ex) {
                if (ex.Status != WebExceptionStatus.ProtocolError) throw;
                return SocialHttpResponse.GetFromWebResponse(ex.Response as HttpWebResponse);
            }

        }
        /// <summary>
        /// Makes a POST request to the MailChimp API. If the <code>AccessToken</code> property has been specified, the
        /// access token will added as an authorization header of the request.
        /// </summary>
        /// <param name="url">The URL to call.</param>
        /// <param name="query">The query string of the request.</param>
        /// <param name="postData">The POST data.</param>
        /// <param name="isMultipart">If <code>true</code>, the content type of the request will be <code>multipart/form-data</code>, otherwise <code>application/x-www-form-urlencoded</code>.</param>
        /// <returns>Returns an instance of <code>SocialHttpResponse</code> wrapping the response from the MailChimp API.</returns>
        public SocialHttpResponse DoHttpPostRequest(string url, SocialQueryString query, SocialPostData postData, bool isMultipart) {

            // Throw an exception if the URL is empty
            if (String.IsNullOrWhiteSpace(url)) throw new ArgumentNullException("url");

            // Initialize a new instance of SocialQueryString if the one specified is NULL
            if (query == null) query = new SocialQueryString();

            // Append the access token to the query string if present in the client and not already
            // specified in the query string
            if (!query.ContainsKey("access_token") && !String.IsNullOrWhiteSpace(AccessToken)) {
                query.Add("access_token", AccessToken);
            }

            // Append the query string to the URL
            if (!query.IsEmpty) url += (url.Contains("?") ? "&" : "?") + query;

            // Initialize a new HTTP request
            SocialHttpRequest request = new SocialHttpRequest {
                Url = url,
                Method = "POST"
            };

            // Add the authorization header if the "AccessToken" property is specified
            if (!String.IsNullOrWhiteSpace(AccessToken)) {
                request.Authorization = "OAuth " + AccessToken;
            }

            // Get the HTTP response
            return request.GetResponse();

        }
Esempio n. 8
0
        /// <summary>
        /// Makes a POST request to the specified <code>url</code>.
        /// </summary>
        /// <param name="url">The URL of the request.</param>
        /// <param name="query">The query string of the request.</param>
        /// <param name="postData">The POST data of the request.</param>
        /// <param name="isMultipart">Indicates the request should be encoded as <code>multipart/form-data</code>.</param>
        /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns>
        public virtual SocialHttpResponse DoHttpPostRequest(string url, SocialQueryString query, SocialPostData postData, bool isMultipart)
        {
            // Some input validation
            if (String.IsNullOrWhiteSpace(url))
            {
                throw new ArgumentNullException("url");
            }

            // Initialize the request
            SocialHttpRequest request = new SocialHttpRequest {
                Method      = SocialHttpMethod.Post,
                Url         = url,
                QueryString = query,
                PostData    = postData,
                IsMultipart = isMultipart
            };

            // Do something extra for the request
            PrepareHttpRequest(request);

            // Make the request to the specified URL
            return(request.GetResponse());
        }