考虑以下代码:
@H_502_2@MultipartFormDataContent MPFD = new MultipartFormDataContent();
MPFD.Add(new StringContent(0.ToString()),"doesNotWork");
MPFD.Add(new StringContent(0.ToString()),"works[]");
HttpClient apiClient = new HttpClient();
var Result = apiClient.PostAsync(testurl,MPFD).Result;
Console.WriteLine("Response: " + Result.Content.ReadAsStringAsync().Result);
然后将此数据发布到Generic Handler,它执行以下操作:
@H_502_2@context.Response.ContentType = "text/plain"; foreach (string key in context.Request.Form.AllKeys) { context.Response.Write(key + ": " + context.Request.Form[key] + Environment.NewLine); }如果我这样做,只会发布以“[]”结尾的名称,但我还需要能够在没有“[]”的情况下发布名称.知道为什么会出现这种行为以及如何解决这个问题?
注意:使用FormUrlEncodedContent时,不会发布没有[]的名称.
解决方法
@H_502_2@form.Add(new StringContent(Opts.DataType),"\"fieldname\"");
form.Add(new ByteArrayContent(mycontent,mycontent.Length),"\"filename\"","filename.csv");
似乎没必要,但它确实有效.