コード例 #1
0
        public T Post <T>(IUploadRequest <T> request) where T : IWeiboResponse, new()
        {
            var url = new StringBuilder(request.Url);

            if (request.WeiboType == WeiboType.Sina)
            {
                url.Append(_format);
            }

            var form = new HttpMultipartMimeForm();

            if (request.WeiboType == WeiboType.QQ)
            {
                form.AppendValue("format", _format.ToString());
            }

            if (request.Parameters.Count > 0)
            {
                foreach (var parameter in request.Parameters)
                {
                    form.AppendValue(parameter.Key, parameter.Value);
                }
            }

            if (request.FileParameters.Count > 0)
            {
                foreach (var fileParameter in request.FileParameters)
                {
                    form.AppendFile(fileParameter.Key, fileParameter.Value);
                }
            }

            var oauthRequest = new OAuthHttpRequestMessage("POST", url.ToString(), form).Sign(_accessToken);
            var result       = _client.Send(oauthRequest).ReadContentAsString();

            var response = new T();

            response.ConvertFrom(result);

            return(response);
        }
コード例 #2
0
        /// <summary>
        ///  This does the real work of the plugin - uploading to imgur.com.
        /// </summary>
        ///
        /// <remarks>
        ///   First upload the main image, and then place the raw
        ///   image URL onto the clipboard for easy reference/paste.
        /// </remarks>
        private void UploadImage()
        {
            Tracing.Trace("ImageShack::UploadImage");

            try
            {
                string relativeUrl = "upload_api.php";
                var    http        = new HttpClient(_baseUri);
                var    form        = new HttpMultipartMimeForm();
                using (var fs = File.Open(this._fileName, FileMode.Open, FileAccess.Read))
                {
                    string mimetype = GetMimeType(this._fileName);
                    form.Add("fileupload",
                             this._fileName,
                             HttpContent.Create(fs, mimetype, fs.Length));
                    form.Add("key", _developerKey);
                    form.Add("rembar", "1");

                    if (PluginSettings.UseCookie)
                    {
                        // TODO: Apply the cookie to the transaction
                    }

                    if (PluginSettings.UseCustomTags)
                    {
                        // prompt for the custom tags here
                        var f     = new System.Windows.Forms.Form();
                        var btnOK = new System.Windows.Forms.Button();
                        var label = new System.Windows.Forms.Label();
                        var txt   = new System.Windows.Forms.TextBox();
                        label.Text                    = "Tags:";
                        label.AutoSize                = true;
                        label.Location                = new System.Drawing.Point(4, 8);
                        txt.Text                      = "";
                        txt.TabIndex                  = 11;
                        txt.Location                  = new System.Drawing.Point(48, 6);
                        txt.Size                      = new System.Drawing.Size(274, 26);
                        btnOK.DialogResult            = System.Windows.Forms.DialogResult.OK;
                        btnOK.Location                = new System.Drawing.Point(254, 32);
                        btnOK.Name                    = "btnOK";
                        btnOK.Size                    = new System.Drawing.Size(68, 23);
                        btnOK.TabIndex                = 61;
                        btnOK.Text                    = "&OK";
                        btnOK.UseVisualStyleBackColor = true;
                        f.Controls.Add(label);
                        f.Controls.Add(txt);
                        f.Controls.Add(btnOK);
                        f.Name = "Tags";
                        f.Text = "Tags for this image?";
                        //f.ClientSize = new System.Drawing.Size(324, 118);
                        f.MinimumSize = new System.Drawing.Size(342, 96);
                        f.MaximumSize = new System.Drawing.Size(342, 96);
                        var result = f.ShowDialog();

                        if (result == DialogResult.OK)
                        {
                            if (!String.IsNullOrEmpty(txt.Text))
                            {
                                form.Add("tags", txt.Text);
                            }
                        }
                        else
                        {
                            Tracing.Trace("cancelled.");
                            return;
                        }
                    }
                    else if (!String.IsNullOrEmpty(PluginSettings.FixedTags))
                    {
                        form.Add("tags", PluginSettings.FixedTags);
                    }


                    var response = http.Post(relativeUrl, form.CreateHttpContent());
                    response.EnsureStatusIsSuccessful();
                    var foo = response.Content.ReadAsXmlSerializable <UploadResponseSuccess>();
                    if ((foo == null) || foo.links == null)
                    {
                        var foo2 = response.Content.ReadAsXmlSerializable <UploadResponseError>();
                        if ((foo2 != null) && (foo2.error != null))
                        {
                            throw new InvalidOperationException(String.Format("Error on upload, id = {0}}, message = {1}.", foo2.error.id, foo2.error.message));
                        }


                        throw new InvalidOperationException("Successful response, but cannot deserialize xml.");
                    }

                    string rawImageUri = foo.links.imageuri;

                    if (PluginSettings.PopBrowser)
                    {
                        System.Diagnostics.Process.Start(rawImageUri);
                    }

                    Clipboard.SetDataObject(rawImageUri, true);

                    Tracing.Trace("all done.");
                    Tracing.Trace("---------------------------------");
                }
            }
            catch (Exception exception2)
            {
                Tracing.Trace("Exception.");
                Tracing.Trace("---------------------------------");
                MessageBox.Show("There's been an exception uploading your image:" +
                                Environment.NewLine +
                                exception2.Message +
                                Environment.NewLine +
                                Environment.NewLine +
                                "You will have to upload this file manually: " +
                                Environment.NewLine +
                                this._fileName,
                                "Failed to upload to ImageShack",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            return;
        }
コード例 #3
0
ファイル: Plugin.cs プロジェクト: brhinescot/CropperPlugins
        /// <summary>
        ///  This does the real work of the plugin - uploading to imgur.com.
        /// </summary>
        ///
        /// <remarks>
        ///   First upload the main image, and then place the raw
        ///   image URL onto the clipboard for easy reference/paste.
        /// </remarks>
        private void UploadImage()
        {
            Tracing.Trace("Imgur::UploadImage");

            if (!VerifyBasicSettings())
            {
                return;
            }

            Hacks.BootstrapSettings(PluginSettings);

            try
            {
                var http = new HttpClient(Plugin._baseUri);
                var form = new HttpMultipartMimeForm();
                using (var fs = File.Open(this._fileName, FileMode.Open, FileAccess.Read))
                {
                    form.Add("key", PluginSettings.Key);
                    form.Add("image",
                             this._fileName,
                             HttpContent.Create(fs, "application/octet-stream", fs.Length));
                    form.Add("type", "file");
                    form.Add("title", "uploaded by Cropper SendToImgur plugin"); // optional
                    form.Add("caption", "http://cropper.codeplex.com");          // optional
                    var response = http.Post("upload.xml", form.CreateHttpContent());
                    response.EnsureStatusIsSuccessful();
                    var foo = response.Content.ReadAsXmlSerializable <UploadResponse>();
                    if (foo.links == null)
                    {
                        throw new InvalidOperationException("Successful response, but link is empty.");
                    }

                    string rawImageUri = foo.links.original;

                    if (PluginSettings.PopBrowser)
                    {
                        System.Diagnostics.Process.Start(rawImageUri);
                    }

                    Clipboard.SetDataObject(rawImageUri, true);
                    if (this._logger != null)
                    {
                        try
                        {
                            this._logger.Log(rawImageUri);
                        }
                        catch (Exception ex2)
                        {
                            MessageBox.Show("There's been an exception writing the ImgUr log?" +
                                            Environment.NewLine +
                                            ex2.Message +
                                            Environment.NewLine,
                                            "This isn't serious",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Information);
                        }
                    }
                }
            }
            catch (Exception exception2)
            {
                Tracing.Trace("Exception: {0}", exception2.StackTrace);
                Tracing.Trace("---------------------------------");
                MessageBox.Show("There's been an exception uploading your image:" +
                                Environment.NewLine +
                                exception2.Message +
                                Environment.NewLine +
                                Environment.NewLine +
                                "You will have to upload this file manually: " +
                                Environment.NewLine +
                                this._fileName,
                                "Upload to Imgur failed",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            return;
        }