Beispiel #1
0
        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 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);
        }
        /// <summary>
        /// Writes the value to the specified <code>stream</code>.
        /// </summary>
        /// <param name="stream">The stream the value should be written to.</param>
        /// <param name="boundary">The multipart boundary.</param>
        /// <param name="newLine">The characters used to indicate a new line.</param>
        /// <param name="isLast">Whether the value is the last in the request body.</param>
        public void WriteToMultipartStream(Stream stream, string boundary, string newLine, bool isLast)
        {
            SocialPostData.Write(stream, "--" + boundary + newLine);
            SocialPostData.Write(stream, "Content-Disposition: form-data; name=\"" + Name + "\"" + newLine);
            SocialPostData.Write(stream, newLine);

            SocialPostData.Write(stream, Value);

            SocialPostData.Write(stream, newLine);
            SocialPostData.Write(stream, "--" + boundary + (isLast ? "--" : "") + newLine);
        }
Beispiel #4
0
        /// <summary>
        /// Makes a signed POST request to the specified <code>url</code>.
        /// </summary>
        /// <param name="url">The URL to call.</param>
        /// <param name="options">The options for the call to the API.</param>
        /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the raw response.</returns>
        public virtual SocialHttpResponse DoHttpPostRequest(string url, IPostOptions options)
        {
            SocialQueryString query    = (options == null ? null : options.GetQueryString());
            SocialPostData    postData = (options == null ? null : options.GetPostData());

            NameValueCollection nvcQuery    = (query == null ? null : query.NameValueCollection);
            NameValueCollection nvcPostData = (postData == null ? null : postData.ToNameValueCollection());

            // TODO: Converting the POST data to a NameValueCollection will not support multipart data

            return(DoHttpRequest("POST", url, nvcQuery, nvcPostData));
        }
Beispiel #5
0
        /// <summary>
        /// Writes the value to the specified <code>stream</code>.
        /// </summary>
        /// <param name="stream">The stream the value should be written to.</param>
        /// <param name="boundary">The multipart boundary.</param>
        /// <param name="newLine">The characters used to indicate a new line.</param>
        /// <param name="isLast">Whether the value is the last in the request body.</param>
        public void WriteToMultipartStream(Stream stream, string boundary, string newLine, bool isLast)
        {
            SocialPostData.Write(stream, "--" + boundary + newLine);
            SocialPostData.Write(stream, "Content-Disposition: form-data; name=\"" + Name + "\"; filename=\"" + FileName + "\"" + newLine);
            SocialPostData.Write(stream, "Content-Type: " + ContentType + newLine);
            SocialPostData.Write(stream, newLine);

            stream.Write(Data, 0, Data.Length);

            SocialPostData.Write(stream, newLine);
            SocialPostData.Write(stream, newLine);
            SocialPostData.Write(stream, "--" + boundary + (isLast ? "--" : "") + newLine);
        }
        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);
        }
Beispiel #7
0
        /// <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>
        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 HTTP GET request to the specified <code>url</code>.
 /// </summary>
 /// <param name="url">The URL of the request.</param>
 /// <param name="postData">The POST data of the request.</param>
 /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns>
 public static SocialHttpResponse DoHttpPostRequest(string url, SocialPostData postData)
 {
     return(DoHttpRequest(url, SocialHttpMethod.Post, null, postData));
 }
            /// <summary>
            /// Makes a HTTP request using the specified <code>url</code> and <code>method</code>.
            /// </summary>
            /// <param name="url">The URL of the request.</param>
            /// <param name="method">The HTTP method of the request.</param>
            /// <param name="query">The query string of the request.</param>
            /// <param name="postData">The POST data of the request.</param>
            /// <returns>Returns an instance of <see cref="SocialHttpResponse"/> representing the response.</returns>
            private static SocialHttpResponse DoHttpRequest(string url, SocialHttpMethod method, SocialQueryString query, SocialPostData postData)
            {
                // Initialize the request
                SocialHttpRequest request = new SocialHttpRequest {
                    Url         = url,
                    Method      = method,
                    QueryString = query,
                    PostData    = postData
                };

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