MultipartFormDataContent.Add StringContent 正在向名称中添加 carraige return/linefeed
MultipartFormDataContent.Add StringContent is adding carraige return/linefeed to the name
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.1
Host: example.com
Content-Type: multipart/form-data; boundary="93655e5a-b6b3-48d6-82c9-0d9aa99164cc"
Content-Length: 522
--93655e5a-b6b3-48d6-82c9-0d9aa99164cc
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=oth_id
1234
--93655e5a-b6b3-48d6-82c9-0d9aa99164cc
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name=job_id
1234
--93655e5a-b6b3-48d6-82c9-0d9aa99164cc
Content-Type: application/octet-stream
Content-Disposition: form-data; name=dt_file; filename=myfile.txt; filename*=utf-8''myfile.txt
a,b,c,d
aa,"b,b","c
c",dd
aaa
--93655e5a-b6b3-48d6-82c9-0d9aa99164cc--
查看 name
值。
查看 RFC 7578 我可以在每个示例中看到,name 的值总是被引用。
Content-Disposition: form-data; name="user"
我没有找到任何提示是否必须引用这些值,所以我无法判断这里谁错了。
要获得此类引用的名称值,您只需在代码中引用这些值。
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 这个
POST https://example.com/ HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary="c33cdc86-db44-40ef-8e6e-3e13a96218d1"
Content-Length: 528
--c33cdc86-db44-40ef-8e6e-3e13a96218d1
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="oth_id"
1234
--c33cdc86-db44-40ef-8e6e-3e13a96218d1
Content-Type: text/plain; charset=utf-8
Content-Disposition: form-data; name="job_id"
1234
--c33cdc86-db44-40ef-8e6e-3e13a96218d1
Content-Type: application/octet-stream
Content-Disposition: form-data; name="dt_file"; filename=myfile.txt; filename*=utf-8''myfile.txt
a,b,c,d
aa,"b,b","c
c",dd
aaa
--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)}\"";
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.1 Host: example.com Content-Type: multipart/form-data; boundary="93655e5a-b6b3-48d6-82c9-0d9aa99164cc" Content-Length: 522 --93655e5a-b6b3-48d6-82c9-0d9aa99164cc Content-Type: text/plain; charset=utf-8 Content-Disposition: form-data; name=oth_id 1234 --93655e5a-b6b3-48d6-82c9-0d9aa99164cc Content-Type: text/plain; charset=utf-8 Content-Disposition: form-data; name=job_id 1234 --93655e5a-b6b3-48d6-82c9-0d9aa99164cc Content-Type: application/octet-stream Content-Disposition: form-data; name=dt_file; filename=myfile.txt; filename*=utf-8''myfile.txt a,b,c,d aa,"b,b","c c",dd aaa --93655e5a-b6b3-48d6-82c9-0d9aa99164cc--
查看 name
值。
查看 RFC 7578 我可以在每个示例中看到,name 的值总是被引用。
Content-Disposition: form-data; name="user"
我没有找到任何提示是否必须引用这些值,所以我无法判断这里谁错了。
要获得此类引用的名称值,您只需在代码中引用这些值。
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 这个
POST https://example.com/ HTTP/1.1 Host: example.com Content-Type: multipart/form-data; boundary="c33cdc86-db44-40ef-8e6e-3e13a96218d1" Content-Length: 528 --c33cdc86-db44-40ef-8e6e-3e13a96218d1 Content-Type: text/plain; charset=utf-8 Content-Disposition: form-data; name="oth_id" 1234 --c33cdc86-db44-40ef-8e6e-3e13a96218d1 Content-Type: text/plain; charset=utf-8 Content-Disposition: form-data; name="job_id" 1234 --c33cdc86-db44-40ef-8e6e-3e13a96218d1 Content-Type: application/octet-stream Content-Disposition: form-data; name="dt_file"; filename=myfile.txt; filename*=utf-8''myfile.txt a,b,c,d aa,"b,b","c c",dd aaa --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)}\"";