使用HttpWebRequest的使用多/ form-data的POST数据/上传图片 [英] Using HttpWebRequest to POST data/upload image using multipart/form-data
问题描述
我试图使用ImageShack API来上传图片。要使用它,我应该 POST
使用图像的multipart / form-data的
。我喜欢它...
VAR POSTDATA =;
VAR REQ = HttpWebRequest.Create(http://www.imageshack.us/upload_api.php);
req.Method =POST;
req.ContentType =的multipart / form-data的;
POSTDATA + =键= my_key_here&安培;;
POSTDATA + =类型的base64 =&安培;;//从图像数据的base64
字节[]字节= File.ReadAllBytes(@D:\\ tmp目录\\ WpfApplication1 \\ WpfApplication1 \\图片\\ Icon128.gif);
字符串连接codeD = Convert.ToBase64String(字节);
POSTDATA + =文件上传=EN + codeD;字节[] = reqData Encoding.UTF8.GetBytes(POSTDATA);
使用(流数据流= req.GetRequestStream())
{
dataStream.Write(reqData,0,reqData.Length);
}VAR解析度=(HttpWebResponse)req.GetResponse();
变种resStream = res.GetResponseStream();
VAR读者=新的StreamReader(resStream);
字符串resString = reader.ReadToEnd();
txt1.Text = resString;
但ImageShack的抱怨
<链接和GT;
<错误ID =parameter_missing>对不起,但我们检测到意外的数据被接收。必要参数'文件上传'丢失或您的文章没有的multipart / form-data的< /错误>
< /链接和GT;
文件上传为present和我使用的multipart / form-data的
什么是错?
更新:
新的code http://pastebin.com/TN6e0CD8
发表的数据 http://pastebin.com/fYE9fsxs
更新2
我看了看其他的问题<一href=\"http://stackoverflow.com/questions/219827/multipart-forms-from-c-client\">http://stackoverflow.com/questions/219827/multipart-forms-from-c-client.修改了我的code。与边界,去掉了预期100头还是我不能得到它的工作...
ServicePointManager.Expect100Continue = FALSE;
VAR边界=----------------------------- 28520690214962;
VAR NEWLINE = Environment.NewLine;
VAR propFormat =边界+换行+
内容处置:表格数据;名称= \\{0} \\+换行+换行+
{1}+换行+换行;
VAR fileHeaderFormat =边界+换行+
内容处置:表格数据;名称= \\{0} \\;文件名= \\{1} \\+换行;VAR REQ =(HttpWebRequest的)HttpWebRequest.Create(HTTP://jm/php/upload.php);
req.Method = WebRequestMethods.Http.Post;
req.ContentType =的multipart / form-data的;边界=+边界;使用(VAR reqStream = req.GetRequestStream()){
VAR reqWriter =新的StreamWriter(reqStream);
VAR TMP =的String.Format(propFormat,STR1,世界你好);
reqWriter.Write(TMP);
TMP =的String.Format(propFormat,STR2赛车,世界你好2);
reqWriter.Write(TMP);
reqWriter.Write(边界+ - );
reqWriter.Flush();
}
VAR解析度= req.GetResponse();
使用(VAR resStream = res.GetResponseStream()){
VAR读者=新的StreamReader(resStream);
txt1.Text = reader.ReadToEnd();
}
我相信你没有正确建立请求主体。
首先,你需要在内容类型头部分边界(随机文本)。例如,
内容类型:多重/ form-data的;
边界= ---- WebKitFormBoundarySkAQdHysJKel8YBM
块引用>现在请求主体的格式将是这样
------ WebKitFormBoundarySkAQdHysJKel8YBM
内容处置:表格数据;名称=钥匙KeyValueGoesHere
------ WebKitFormBoundarySkAQdHysJKel8YBM
内容处置:表格数据;名称=参数2ValueHere
------ WebKitFormBoundarySkAQdHysJKel8YBM
内容处置:表格数据;名称=文件上传;文件名=y1.jpg
内容类型:图像/ JPEG[图像数据放在这里]我会建议你使用的工具,如提琴手了解这些请求是如何构建的。
I am trying to use the ImageShack API to upload images. To use it, I am supposed to
POST
the image usingmultipart/form-data
. I did it like ...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&"; postData += "type=base64&"; // 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;
but ImageShack is complaining that
<links> <error id="parameter_missing">Sorry, but we've detected that unexpected data is received. Required parameter 'fileupload' is missing or your post is not multipart/form-data</error> </links>
FileUpload is present and I am using
multipart/form-data
whats wrong?UPDATE:
New Code http://pastebin.com/TN6e0CD8
Post data http://pastebin.com/fYE9fsxs
UPDATE 2
i looked at the other question http://stackoverflow.com/questions/219827/multipart-forms-from-c-client. modified my code with boundary, removed the expect 100 header still i cant get it working ...
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(); }
解决方案I believe that you are not building the request body correctly. First, you need to include part boundary (random text) in content type header. For example,
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarySkAQdHysJKel8YBM
Now format of request body will be something like
------WebKitFormBoundarySkAQdHysJKel8YBM Content-Disposition: form-data;name="key" KeyValueGoesHere ------WebKitFormBoundarySkAQdHysJKel8YBM Content-Disposition: form-data;name="param2" ValueHere ------WebKitFormBoundarySkAQdHysJKel8YBM Content-Disposition: form-data;name="fileUpload"; filename="y1.jpg" Content-Type: image/jpeg [image data goes here]
I will suggest you to use tool such as Fiddler to understand how these requests are built.
这篇关于使用HttpWebRequest的使用多/ form-data的POST数据/上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!