0

我需要编写一个完整的 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);

客户端代表与我的网络服务的连接。这是 HttpRequestBody 函数:

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

return  res;
} 

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

4

1 回答 1

1

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

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

于 2013-10-07T03:34:05.953 回答