gpt4 book ai didi

c# - MultipartFormDataContent.Add String Content 正在向名称添加回车符/换行符

转载 作者:行者123 更新时间:2023-11-30 18:11:40 32 4
gpt4 key购买 nike

formData.Add(sJobId,"job_id");正在向服务器发送“job_id\r\n”

这是我的 C# 方法:

public static async Task UploadAsync(string url, int job_id, string filename, string filePath) {
try {
// Submit the form using HttpClient and
// create form data as Multipart (enctype="multipart/form-data")
using (var fileStream = new StreamContent(System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read)))
using (var formData = new MultipartFormDataContent()) {
StringContent sJobId = new StringContent(job_id.ToString());
StringContent sOthId = new StringContent(job_id.ToString());
// Try as I might C# adds a CrLf to the end of the job_id tag - so have to strip it in ruby
formData.Add(sOthId, "oth_id");
formData.Add(sJobId,"job_id");
fileStream.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
formData.Add(fileStream, "dt_file", filename);
HttpResponseMessage response = await HttpClient.PostAsync(url, formData);

// If the upload failed there is not a lot we can do
return;
}
} catch (Exception ex) {
// Not a lot we can do here - so just ignore it
System.Diagnostics.Debug.WriteLine($"Upload failed {ex.Message}");
}

}

这是我的 Ruby puma 服务器正在接收的内容 - 查看 oth_id 和 job_id 是如何附加的,\r\n 但“dt_file”没有。

Parameters: {"oth_id\r\n"=>"42157", "job_id\r\n"=>"42157", "dt_file"=>#<ActionDispatch::Http::UploadedFile:0x007f532817dc98 @tempfile=#<Tempfile:/tmp/RackMultipart20190715-37897-189ztb6.msg>, @original_filename="2019-07-15 164600.msg", @content_type="application/octet-stream", @headers="Content-Type: application/octet-stream\r\nContent-Disposition: form-data; name=dt_file; filename=\"2019-07-15 164600.msg\"; filename*=utf-8''2019-07-15%20164600.msg\r\n">}

如何停止 formData.Add 将\r\n 附加到名称?

最佳答案

应用程序发送到服务器的原始消息是

POST https://example.com/ HTTP/1.1Host: example.comContent-Type: multipart/form-data; boundary="93655e5a-b6b3-48d6-82c9-0d9aa99164cc"Content-Length: 522--93655e5a-b6b3-48d6-82c9-0d9aa99164ccContent-Type: text/plain; charset=utf-8Content-Disposition: form-data; name=oth_id1234--93655e5a-b6b3-48d6-82c9-0d9aa99164ccContent-Type: text/plain; charset=utf-8Content-Disposition: form-data; name=job_id1234--93655e5a-b6b3-48d6-82c9-0d9aa99164ccContent-Type: application/octet-streamContent-Disposition: form-data; name=dt_file; filename=myfile.txt; filename*=utf-8''myfile.txta,b,c,daa,"b,b","cc",ddaaa--93655e5a-b6b3-48d6-82c9-0d9aa99164cc--

Have a look at the name values.

Looking at RFC 7578 I can see in every example, that the value for name is always quoted.

Content-Disposition: form-data; name="user"

I did not find any hint if it is mandantory or not to quote the values, so I cannot judge who is wrong here.

To get such quoted name values you only have to quote the values in code.

public static async Task UploadAsync(string url, int job_id, string filename, string filePath) {
try {
// Submit the form using HttpClient and
// create form data as Multipart (enctype="multipart/form-data")
using (var fileStream = new StreamContent(System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read)))
using (var formData = new MultipartFormDataContent()) {
StringContent sJobId = new StringContent(job_id.ToString());
StringContent sOthId = new StringContent(job_id.ToString());
// Try as I might C# adds a CrLf to the end of the job_id tag - so have to strip it in ruby
formData.Add(sOthId, "\"oth_id\"");
formData.Add(sJobId,"\"job_id\"");
fileStream.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
formData.Add(fileStream, "\"dt_file\"", filename);
HttpResponseMessage response = await HttpClient.PostAsync(url, formData);

// If the upload failed there is not a lot we can do
return;
}
} catch (Exception ex) {
// Not a lot we can do here - so just ignore it
System.Diagnostics.Debug.WriteLine($"Upload failed {ex.Message}");
}

}

现在将发布这个

POST https://example.com/ HTTP/1.1Host: example.comContent-Type: multipart/form-data; boundary="c33cdc86-db44-40ef-8e6e-3e13a96218d1"Content-Length: 528--c33cdc86-db44-40ef-8e6e-3e13a96218d1Content-Type: text/plain; charset=utf-8Content-Disposition: form-data; name="oth_id"1234--c33cdc86-db44-40ef-8e6e-3e13a96218d1Content-Type: text/plain; charset=utf-8Content-Disposition: form-data; name="job_id"1234--c33cdc86-db44-40ef-8e6e-3e13a96218d1Content-Type: application/octet-streamContent-Disposition: form-data; name="dt_file"; filename=myfile.txt; filename*=utf-8''myfile.txta,b,c,daa,"b,b","cc",ddaaa--c33cdc86-db44-40ef-8e6e-3e13a96218d1--

刚找到一个pull request for PowerShell

// .NET does not enclose field names in quotes, however, modern browsers and curl do.
contentDisposition.Name = $"\"{LanguagePrimitives.ConvertTo<String>(fieldName)}\"";

关于c# - MultipartFormDataContent.Add String Content 正在向名称添加回车符/换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57033535/

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