写入完整的 HTTP POST 请求数据包



我需要编写一个完整的http请求来调用SOAP服务。我没有用于肥皂请求的库,所以我需要编写完整的 HTTP 数据包。这就是我进行的方式(我正在编写Arduino板):

String body = HttpRequestBody("33", "77%");
client.println("POST /dataserver HTTP/1.1");
client.println("Accept: text/xml, multipart/related");
client.println("Content-Type: text/xml; charset=utf-8");
client.println("SOAPAction: "http://example.com/Functions/SendDataRequest"");
client.println("User-Agent: Arduino WiFiShield");
client.println("Content-Length: "+body.length());
client.println("Host: arduino-data-server.appspot.com/dataserver");
client.println("Connection: Keep-Alive");
client.println();
client.println(body);

客户端表示与我的 Web 服务的连接。这是 HttpRequestBody 函数:

String HttpRequestBody(String v1, String v2) {
Serial.println("Generating xml message...");
String res = "";
res += "<?xml version="1.0"?>nr";
res +="<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"nr";
res +="<S:Body>nr";
res +="<ns2:sendData xmlsn:ns2="http://example.com">nr";
res +="<arg0>"+v1+"</arg0>nr";
res +="<arg1>"+v2+"</arg1>nr";
res +="</ns2:sendData>nr";
res +="</S:Body>nr";
res +="</S:Envelope>nr";
Serial.println(res);
return  res;
} 

但是出了点问题,我无法联系网络服务器。Web服务器可以工作并且它是可访问的,因为如果我将POST更改为GET,在Web服务日志上,我会看到连接。我该如何解决它?

在 HttpRequestBody 中,您正在分配: String res = "";然后更新它。 最后,你返回res. 但是,res是在 HttpRequestBody (??) 堆栈上分配的,但不能保证它在 HttpRequestbody 终止后会在那里。

您可能需要执行 C 代码中使用的 malloc C++等效操作,以确保 res 在堆上并且不会被释放。

最新更新