C#: Using HttpWebRequest to POST data/upload image using multipart/form-data
<p>I am trying to use the ImageShack API to upload images. To use it, I am supposed to <code>POST</code> the image using <code>multipart/form-data</code>. I did it like ...</p>

<pre><code>var postData = "";
var req = HttpWebRequest.Create("http://www.imageshack.us/upload_api.php");
req.Method = "POST";
req.ContentType = "multipart/form-data";
postData += "key=my_key_here&amp;";
postData += "type=base64&amp;";

// get base64 data from image
byte[] bytes = File.ReadAllBytes(@"D:\tmp\WpfApplication1\WpfApplication1\Images\Icon128.gif");
string encoded = Convert.ToBase64String(bytes);
postData += "fileupload=" + encoded;

byte[] reqData = Encoding.UTF8.GetBytes(postData);
using (Stream dataStream = req.GetRequestStream())
{
    dataStream.Write(reqData, 0, reqData.Length);
}

var res = (HttpWebResponse)req.GetResponse();
var resStream = res.GetResponseStream();
var reader = new StreamReader(resStream);
string resString = reader.ReadToEnd();
txt1.Text = resString;
</code></pre>

<p>but ImageShack is complaining that </p>

<pre><code>&lt;links&gt;
    &lt;error id="parameter_missing"&gt;Sorry, but we've detected that unexpected data is received. Required parameter 'fileupload' is missing or your post is not multipart/form-data&lt;/error&gt;
&lt;/links&gt;
</code></pre>

<p>FileUpload is present and I am using <code>multipart/form-data</code> whats wrong?</p>

<p><strong>UPDATE:</strong> </p>

<p>New Code <a href="http://pastebin.com/TN6e0CD8" rel="nofollow">http://pastebin.com/TN6e0CD8</a></p>

<p>Post data <a href="http://pastebin.com/fYE9fsxs" rel="nofollow">http://pastebin.com/fYE9fsxs</a></p>

<p><strong>UPDATE 2</strong></p>

<p>i looked at the other question <a href="http://stackoverflow.com/questions/219827/multipart-forms-from-c-client">http://stackoverflow.com/questions/219827/multipart-forms-from-c-client</a>. modified my code with boundary, removed the expect 100 header still i cant get it working ... </p>

<pre><code>ServicePointManager.Expect100Continue = false;
var boundary = "-----------------------------28520690214962";
var newLine = Environment.NewLine;
var propFormat = boundary + newLine +
                    "Content-Disposition: form-data; name=\"{0}\"" + newLine + newLine + 
                    "{1}" + newLine + newLine;
var fileHeaderFormat = boundary + newLine +
                        "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + newLine;

var req = (HttpWebRequest)HttpWebRequest.Create("http://jm/php/upload.php");
req.Method = WebRequestMethods.Http.Post;
req.ContentType = "multipart/form-data; boundary=" + boundary;

using (var reqStream = req.GetRequestStream()) {
    var reqWriter = new StreamWriter(reqStream);
    var tmp = string.Format(propFormat, "str1", "hello world");
    reqWriter.Write(tmp);
    tmp = string.Format(propFormat, "str2", "hello world 2");
    reqWriter.Write(tmp);
    reqWriter.Write(boundary + "--");
    reqWriter.Flush();
}
var res = req.GetResponse();
using (var resStream = res.GetResponseStream()) {
    var reader = new StreamReader(resStream);
    txt1.Text = reader.ReadToEnd();
}
</code></pre>
