Ejemplo n.º 1
0
        public T GetResponse <T>(String method_name, params DiscuzParam[] parameters)
        {
            //String url = FormatGetUrl(method_name, parameters);
            //byte[] response_bytes = GetResponseBytes(url);

            DiscuzParam[] signed = Sign(method_name, parameters);

            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < signed.Length; i++)
            {
                if (i > 0)
                {
                    builder.Append("&");
                }

                builder.Append(signed[i].ToEncodedString());
            }

            byte[] response_bytes = GetResponseBytes(Url, method_name, builder.ToString());


            XmlSerializer response_serializer = GetSerializer(typeof(T));

            try
            {
                T response = (T)response_serializer.Deserialize(new MemoryStream(response_bytes));
                return(response);
            }
            catch
            {
                Error error = (Error)ErrorSerializer.Deserialize(new MemoryStream(response_bytes));
                throw new DiscuzException(error.ErrorCode, error.ErrorMsg);
            }
        }
Ejemplo n.º 2
0
        public T GetResponse <T>(string method_name, params FacebookParam[] parameters)
        {
            string url = FormatGetUrl(method_name, parameters);

            byte[] response_bytes = GetResponseBytes(url);

            XmlSerializer response_serializer = GetSerializer(typeof(T));

            try {
                T response = (T)response_serializer.Deserialize(new MemoryStream(response_bytes));
                return(response);
            }
            catch {
                Error error = (Error)ErrorSerializer.Deserialize(new MemoryStream(response_bytes));
                throw new FacebookException(error.ErrorCode, error.ErrorMsg);
            }
        }
Ejemplo n.º 3
0
        public bool GetBoolResponse(string method_name, params FacebookParam[] parameters)
        {
            string url = FormatGetUrl(method_name, parameters);

            byte[] response_bytes = GetResponseBytes(url);

            XmlDocument doc = new XmlDocument();

            doc.LoadXml(Encoding.Default.GetString(response_bytes));

            try {
                return(System.Convert.ToBoolean(Int32.Parse(doc.InnerText)));
            }
            catch (FormatException e) {
                Error error;
                try {
                    error = (Error)ErrorSerializer.Deserialize(new MemoryStream(response_bytes));
                } catch {
                    Console.Error.WriteLine("Parse error: Inner text was '{0}'", doc.InnerText);
                    throw e;
                }
                throw new FacebookException(error.ErrorCode, error.ErrorMsg);
            }
        }
Ejemplo n.º 4
0
        internal Photo Upload(long aid, string caption, string path, string session_key)
        {
            // check for file
            FileInfo file = new FileInfo(path);

            if (!file.Exists)
            {
                throw new FileNotFoundException("Upload file not found", path);
            }

            // create parameter string
            List <FacebookParam> parameter_list = new List <FacebookParam>();

            parameter_list.Add(FacebookParam.Create("call_id", DateTime.Now.Ticks));
            parameter_list.Add(FacebookParam.Create("session_key", session_key));
            parameter_list.Add(FacebookParam.Create("aid", aid));

            if (caption != null && caption != string.Empty)
            {
                parameter_list.Add(FacebookParam.Create("caption", caption));
            }

            FacebookParam[] param_array  = Sign("facebook.photos.upload", parameter_list.ToArray());
            string          param_string = string.Empty;

            foreach (FacebookParam param in param_array)
            {
                param_string = param_string + "--" + BOUNDARY + LINE;
                param_string = param_string + "Content-Disposition: form-data; name=\"" + param.Name + "\"" + LINE + LINE + param.Value + LINE;
            }

            param_string = param_string + "--" + BOUNDARY + LINE
                           + "Content-Disposition: form-data; name=\"filename\"; filename=\"" + file.Name + "\"" + LINE
                           + "Content-Type: image/jpeg" + LINE + LINE;

            string closing_string = LINE + "--" + BOUNDARY + "--";

            // get bytes
            byte[] param_bytes   = System.Text.Encoding.Default.GetBytes(param_string);
            byte[] closing_bytes = System.Text.Encoding.Default.GetBytes(closing_string);

            byte[] file_bytes = System.IO.File.ReadAllBytes(path);

            // compose
            List <byte> byte_list = new List <byte>(param_bytes.Length + file_bytes.Length + closing_bytes.Length);

            byte_list.AddRange(param_bytes);
            byte_list.AddRange(file_bytes);
            byte_list.AddRange(closing_bytes);

            byte[] final_bytes = new byte[byte_list.Count];
            byte_list.CopyTo(final_bytes);

            // create request
            WebClient wc = new WebClient();

            wc.Headers.Set("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            wc.Headers.Add("MIME-version", "1.0");

            // upload
            byte[] response = wc.UploadData("http://api.facebook.com/restserver.php?", "POST", final_bytes);

            // deserialize
            XmlSerializer photo_serializer = new XmlSerializer(typeof(Photo));

            try {
                return((Photo)photo_serializer.Deserialize(new MemoryStream(response)));
            }
            catch {
                Error error = (Error)ErrorSerializer.Deserialize(new MemoryStream(response));
                throw new FacebookException(error.ErrorCode, error.ErrorMsg);
            }
        }