Xamarin.Android:具有MultipartFormDataContent的HttpClient返回400错误请求

时间:2018-06-25 23:41:42

标签: c# xamarin.android httpclient multipartform-data bad-request

我开发了一个 Xamarin.Android应用,我需要更新一个录音文件。

为此,我的客户向我提供了这样的API:

  • URL:/ api / recordings /
  • 方法:开机自检
  • 数据格式:multipart / form-data
  • 字段:一些文本字段和要发送的音频文件

当我在邮递员上对其进行测试时,它正常工作。生成代码段为:

POST /api/recordings/ HTTP/1.1
Host: X.X.X.X
Cache-Control: no-cache
Postman-Token: xxxxx-xxxx-xxxx-xxxx-xxxxx
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="param1" 
param1 name

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="param2"
param2 name

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="audio_file"; filename="xxx.wav"
Content-Type: audio/wav

但是,当我尝试从该应用程序 访问时,出现了 400-错误的请求错误

首次尝试:

我首先尝试使用以下基本代码:

using (HttpClient client = new HttpClient())
{       
    try
    {
        MultipartFormDataContent formData = new MultipartFormDataContent();
        formData.Add(new StringContent("param1 name"), "param1");
        formData.Add(new StringContent("param2 name"), "param2");
        var response = await client.PostAsync(url, formData);
        Logger.Log("Response : " + responseObj.ToString());
    }
    catch (Exception ex)
    {
        Logger.Log("Exception : " + ex.ToString());
    }
}

但是出现 400-错误的请求错误

Response : StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Server: nginx/1.15.0
  Date: Mon, 25 Jun 2018 23:25:41 GMT
  Connection: keep-alive
  Vary: Cookie
  X-Frame-Options: SAMEORIGIN
  Content-Type: application/json
  Content-Length: 85
  Allow: POST, OPTIONS
}

第二次尝试:

然后,我尝试了以下发现的there片段:

string servResp = "";
string boundary = "----CustomBoundary" + DateTime.Now.Ticks.ToString("x");
using (var content = new MultipartFormDataContent(boundary))
{
    content.Headers.Remove("Content-Type");
    content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
    content.Add(new StringContent("param1 name"), "param1");
    content.Add(new StringContent("param2 name"), "param2");

    HttpClientHandler handler = new HttpClientHandler();
    var cookieContainer = new CookieContainer();
    handler.CookieContainer = cookieContainer;

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
    request.Headers.ExpectContinue = false;
    request.Content = content;
    try
    {
        var httpClient = new HttpClient(handler);
        HttpResponseMessage response = await httpClient.SendAsync(request);
        response.EnsureSuccessStatusCode();
        servResp = await response.Content.ReadAsStringAsync();
    }
    catch (Exception ex)
    {
        Logger.Log("PostTest() - Exception : " + ex.ToString());
    }
}
return servResp;

但是仍然出现 400-错误的请求错误

Exception : System.Net.Http.HttpRequestException: 400 (Bad Request)
  at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode () [0x0002a] in <996a681f30a44cd685a4da54e11956e2>:0 

=>您有什么解释吗?我该如何解决?

编辑:音频文件

我没有指定音频文件的代码,在两种情况下我都使用ByteArrayContent:

byte[] b = System.IO.File.ReadAllBytes(fullFilePath);
formData.Add(new ByteArrayContent(b, 0, b.Length), "audio_file", "audio.wav");

结论:

我能够通过创建一个使用相同服务的控制台项目来解决我的问题:这使我能够查找并修复错误。

对于另一个问题,我跟随Fiddler instructions在设备上进行参数代理。但是,该代理似乎不适用于应用,而仅适用于最新版本的Android网站...

您知道Fiddler的替代品可以在Android设备上运行吗?

0 个答案:

没有答案