WebRequest的POST既文件和参数 [英] WebRequest POST with both file and parameters

查看:1042
本文介绍了WebRequest的POST既文件和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想上传文件和发送以及一些参数来使用.NET / C#我的网站。看了一些教程,要么做一些参数或文件,我已经试过了,没有成功,将它们结合起来。下面是我尝试如何做的:

I'm trying to upload a file and a send along a few parameters to my site using .NET / C#. Having read a few tutorials that do either a few parameters or a file, I've tried, unsuccessfully, to combine them. Here is how I try doing it:

WebRequest req = WebRequest.Create(baseURL + "upload");
req.Credentials = new NetworkCredential(username, password);
String boundary = "B0unD-Ary";
req.ContentType = "multipart/form-data; boundary=" + boundary;
req.Method = "POST";
((HttpWebRequest)req).UserAgent = "UploadTester v0.1";

string postData = "--" + boundary + "\nContent-Disposition: form-data\n";
postData += "myId=123&someFk=456";
postData += "\n--" + boundary + "\nContent-Disposition: form-data; name=\"file\" filename=\"upload.pdf\" Content-Type: application/pdf\n\n";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

byte[] filedata = null;
using (BinaryReader reader = new BinaryReader(File.OpenRead("myfile.pdf")))
    filedata = reader.ReadBytes((int)reader.BaseStream.Length);

req.ContentLength = byteArray.Length + filedata.Length;
req.GetRequestStream().Write(byteArray, 0, byteArray.Length);
req.GetRequestStream().Write(filedata, 0, filedata.Length);

WebResponse response = req.GetResponse();
Stream data = response.GetResponseStream();
StreamReader sReader = new StreamReader(data);
String sResponse = sReader.ReadToEnd();
response.Close();

当我执行它,我得到一个500的异常,说:头一节有超过10240 bnytes(也许它没有正确终止)和Wireshark告诉我,请求发​​送是一种畸形的包,那里的MIME多是格式不正确。

When I execute it, I get a 500 exception, saying "Header section has more than 10240 bnytes (maybe it is not properly terminated)" and Wireshark informs me that the request sent was a malformed package, where the MIME multipart was malformed.

有可能是几个问题在这里,所以请让我知道所有你能发现的问题

There are probably several issues here, so please let me know all the problems you can spot

更新:以MIME从C#/。NET分开,我已经催生了一个线程在这里:<一href="http://stackoverflow.com/questions/1880002/error-in-mime-packet-for-http-post">http://stackoverflow.com/questions/1880002/error-in-mime-packet-for-http-post

Update: to separate MIME from C#/.NET, I've spawned a thread here: http://stackoverflow.com/questions/1880002/error-in-mime-packet-for-http-post

更新2 :那么后端的确与内容长度的问题,他说,阅读可用的字节数比规定的内容长度。但!如果我减少了内容长度在req.Co​​ntentLength因此,我没有一个缓冲区大小发送数据足够大。有什么建议?

Update 2: So the backend indeed has issues with the content-length, saying that the amount of bytes available for reading is smaller than the stated content-length. BUT! If I reduce the content-length in req.ContentLength accordingly, I don't have a buffer size large enough for sending the data. Any suggestions?

更新3 :事实上,它看起来像头有太大的尺寸相比多少数据包含

Update 3: Actually, it looks like the header has a too large size compared to how much data it contains

推荐答案

现在的问题是,你缺少一个'\ N'。下面几行:

The problem is that you're missing a '\n'. The following line:

string postData = "--" + boundary + "\nContent-Disposition: form-data\n";

应该是:

string postData = "--" + boundary + "\nContent-Disposition: form-data\n\n";

这行:

postData += "\n--" + boundary + "\nContent-Disposition: form-data; name=\"file\" filename=\"upload.pdf\" Content-Type: application/pdf\n\n"

失踪前Content-Type的'一'\ N'。它应该是:

is missing a '\n' before 'Content-Type'. It should be:

postData += "\n--" + boundary + "\nContent-Disposition: form-data; name=\"file\" filename=\"upload.pdf\"\nContent-Type: application/pdf\n\n"

这篇关于WebRequest的POST既文件和参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆