MultipartFormDataContent.Add StringContent正在将回车/换行符添加到名称中

时间:2019-07-15 05:10:08

标签: c# .net .net-4.6.1

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。在名称后添加\ r \ n?

1 个答案:

答案 0 :(得分:0)

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

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 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)}\"";