gpt4 book ai didi

c# - 由于 WebClient 的 uploadData 不对数据进行编码,那么向其添加 "Content-Type"、 "multipart/form-data" header 会产生什么影响

转载 作者:太空狗 更新时间:2023-10-29 23:09:12 25 4
gpt4 key购买 nike

C# 的 uploadData方法不对正在发送的数据进行编码。所以,如果我使用这种方法发送一个文件(在将其转换为字节之后),并且接收方正在寻找一个 multiform/form-data post,那么它显然是行不通的。将添加一个标题,如:

WebClient c = new WebClient();
c.Headers.Add("Content-Type", "multipart/form-data");

让它发送加密为多形式的数据,还是数据仍未加密(因此无法被期望多形式数据的服务器解析)?

请注意,我不能使用 WebClient 的 uploadFile,因为我没有权限在客户端获取文件路径位置(我只有一个流,我可以将其转换为字节)

最佳答案

如果您希望它安全,为什么不通过 h​​ttps 使用 WebClient 的 UploadFile?这将自动处理添加 multipart/form-data

使用 UploadFile 的例子

http://msdn.microsoft.com/en-us/library/36s52zhs.aspx

还有一件事,编码和加密是两件不同的事情。

编辑:

如果您在 WebClient 项目中使用 WebClient,您可能应该将您的问题标记为 Silverlight。无论如何,SL 中的 WebClient 类没有任何 UploadData 方法。有关详细信息,请参阅此内容:

http://msdn.microsoft.com/en-us/library/system.net.webclient%28v=vs.95%29.aspx

无论如何,这里是您问题的有效解决方案:

在你的按钮点击中,有这样的代码:

OpenFileDialog dialog = new OpenFileDialog();
bool? retVal = dialog.ShowDialog();
if (retVal.HasValue && retVal == true)
{
using (Stream stream = dialog.File.OpenRead())
{
MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "multipart/form-data; boundary=---------------------------" + _boundaryNo;
webClient.OpenWriteAsync(new Uri("http://localhost:1463/Home/File", UriKind.Absolute), "POST", new { Stream = memoryStream, FileName = dialog.File.Name });
webClient.OpenWriteCompleted += new OpenWriteCompletedEventHandler(webClient_OpenWriteCompleted);
}
}

和事件本身:

void webClient_OpenWriteCompleted(object sender, OpenWriteCompletedEventArgs e)
{
if (e.Error == null)
{
Stream responseStream = e.Result as Stream;
dynamic obj = e.UserState;
MemoryStream memoryStream = obj.Stream as MemoryStream;
string fileName = obj.FileName;
if (responseStream != null && memoryStream != null)
{
string headerTemplate = string.Format("-----------------------------{0}\r\n", _boundaryNo);

memoryStream.Position = 0;
byte[] byteArr = memoryStream.ToArray();
string data = headerTemplate + string.Format("Content-Disposition: form-data; name=\"pic\"; filename=\"{0}\"\r\nContent-Type: application/octet-stream\r\n\r\n", fileName);
byte[] header = Encoding.UTF8.GetBytes(data);
responseStream.Write(header, 0, header.Length);

responseStream.Write(byteArr, 0, byteArr.Length);
header = Encoding.UTF8.GetBytes("\r\n");
responseStream.Write(byteArr, 0, byteArr.Length);

byte[] trailer = System.Text.Encoding.UTF8.GetBytes(string.Format("-----------------------------{0}--\r\n", _boundaryNo));
responseStream.Write(trailer, 0, trailer.Length);
}
memoryStream.Close();
responseStream.Close();
}
}

其中 _boundaryNo 是 private string _boundaryNo = DateTime.Now.Ticks.ToString("x");

我让它与 Asp.Net MVC 4 和 Silverlight 5 一起工作。

祝你好运:)

关于c# - 由于 WebClient 的 uploadData 不对数据进行编码,那么向其添加 "Content-Type"、 "multipart/form-data" header 会产生什么影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12785411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com