c#-通过HttpClient运行HTTP POST时无法识别的响应?

c#-通过HttpClient运行HTTP POST时无法识别的响应?,c#,.net,arduino,esp8266,arduino-esp8266,C#,.net,Arduino,Esp8266,Arduino Esp8266,我遇到了一个问题,当我通过HttpClient运行HTTP POST时。我使用它将固件上传到ESP8266 Arduino的OTA固件升级界面(参见示例:),但它抛出一个异常,表示 “WinHttpException:服务器返回了无效或无法识别的响应” 当我通过cURL上传固件映像时,它完全可以使用下面的命令工作: curl -F "image=@firmware.bin" 192.168.1.104/update 我尝试过用Fiddler进行调试,它告诉我有504网关超时。 但如果我使用cU

我遇到了一个问题,当我通过
HttpClient
运行HTTP POST时。我使用它将固件上传到ESP8266 Arduino的OTA固件升级界面(参见示例:),但它抛出一个异常,表示

“WinHttpException:服务器返回了无效或无法识别的响应”

当我通过cURL上传固件映像时,它完全可以使用下面的命令工作:

curl -F "image=@firmware.bin" 192.168.1.104/update
我尝试过用Fiddler进行调试,它告诉我有504网关超时。 但如果我使用cURL,它就不会发生

我想这可能是由于ESP8266固件OTA更新API中的一些错误造成的,而我的程序没有生成一个能够“满足”API的头,所以它被拒绝了。如果可能的话,我想问一下,这个问题有没有解决办法?提前谢谢

以下是Fiddler捕捉到的旋度原始结果:

POST http://192.168.1.104/update HTTP/1.1
Host: 192.168.1.104
User-Agent: curl/7.51.0
Accept: */*
Connection: Keep-Alive
Content-Length: 335869
Expect: 100-continue
Content-Type: multipart/form-data; boundary=------------------------32e3208a349a700d

--------------------------32e3208a349a700d
Content-Disposition: form-data; name="image"; filename="firmware.bin"
Content-Type: application/octet-stream

.......(Firmware File Content).......
以下是我的程序的原始结果:

POST http://192.168.1.104/update HTTP/1.1
Connection: Keep-Alive
Content-Type: multipart/form-data; boundary="----TwilightFirmware"
Accept-Encoding: gzip, deflate
Content-Length: 335857
Host: 192.168.1.104

------TwilightFirmware
Content-Type: application/octet-stream
Content-Disposition: form-data; name=update; filename=firmware.bin; filename*=utf-8''firmware.bin

.............(Firmware file Content)...........
这是我的(部分)C代码

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.IO;
使用System.Net.Http;
使用系统诊断;
名称空间JasmineApp.Core
{
类LocalFirmwareUpdater
{
公共异步任务上载本地固件(字符串baseUrl、字符串文件路径)
{
Debug.WriteLine(“[Core.FirmwareUpdater]基本URL为:”+baseUrl);
HttpCommandHandler httpHandler=新的HttpCommandHandler();
SetHttpBaseUrl(baseUrl);
字节[]firmwareContent=File.ReadAllBytes(文件路径);
//HttpContent postContent=新的StreamContent(firmwareStream);
HttpContent postContent=新的ByteArrayContent(固件内容);
添加(“内容类型”、“应用程序/八位字节流”);
var formData=新的MultipartFormDataContent(--twillightfirmware);
formData.Add(postContent,“update”,“firmware.bin”);
字符串结果=等待httpHandler.ExecutePostAsync(“/update”,formData);
//字符串结果=等待httpHandler.ExecutePostAsync(“/update”,postContent);
返回(
result.Equals(string.Empty)
||结果==null
||结果。包含(“失败”)?false:true;
}
}
}
…HTTP处理程序代码如下所示:

    public async Task<string> ExecutePostAsync(string pathAndQuery, MultipartFormDataContent content)
    {
        Debug.WriteLine("[Core.HttpHandler] Request POST URL: " + BaseUrl + pathAndQuery);

        using(var client = this.getHttpClient())
        {
            client.Timeout = TimeSpan.FromSeconds(2000000);
            var httpResponseMessage = client.PostAsync(pathAndQuery, content).Result;


            if(httpResponseMessage != null)
            {
                string responseResult = await httpResponseMessage.Content.ReadAsStringAsync();
                Debug.WriteLine("[Core.HttpCommandHandler] POST return message: " + httpResponseMessage.Content.ReadAsStringAsync().Result);
                return responseResult;
            }
            else
            {
                // Return an empty string to avoid exceptions
                // Don't need to worrry if it's empty.
                return string.Empty;
            }
        }
公共异步任务ExecutePostAsync(字符串路径和查询,MultipartFormDataContent)
{
Debug.WriteLine(“[Core.HttpHandler]请求POST URL:”+BaseUrl+pathAndQuery);
使用(var client=this.getHttpClient())
{
client.Timeout=TimeSpan.FromSeconds(2000000);
var httpResponseMessage=client.PostAsync(路径和查询,内容).Result;
如果(httpResponseMessage!=null)
{
string responseResult=await httpResponseMessage.Content.ReadAsStringAsync();
Debug.WriteLine(“[Core.HttpCommandHandler]POST返回消息:”+httpResponseMessage.Content.ReadAsStringAsync().Result);
返回响应结果;
}
其他的
{
//返回空字符串以避免异常
//如果是空的,不用担心。
返回字符串。空;
}
}

顺便说一句,如果有必要,你可以在GitHub上查看我的完整代码。这是链接:

你好,jackson,你应该更改esp软件。你不使用,因为我们上载了服务器端。你可以使用此示例,你将更改此部分

 server.on("/update", HTTP_POST, [](){
  server.sendHeader("Connection", "close");
  server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
  server.sendHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Authorization");
  server.send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
  ESP.restart();
}
我希望解决您的问题:)享受编码的乐趣

尝试使用“图像”名称


我以前试过,实际上还是一样
 server.on("/update", HTTP_POST, [](){
  server.sendHeader("Connection", "close");
  server.sendHeader("Access-Control-Allow-Methods", "GET, POST, PUT");
  server.sendHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, Authorization");
  server.send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
  ESP.restart();
}
formData.Add(postContent, "image", "firmware.bin");