gpt4 book ai didi

c++ - 使用 StringCbPrintf 格式化缓冲区

转载 作者:行者123 更新时间:2023-11-28 03:05:01 25 4
gpt4 key购买 nike

我在如何使用 StringCbPrintf 格式化缓冲区方面遇到困难,这里应该是一个 HTTP 请求:

char getExternalIpRequest[1200]; 
ZeroMemory(getExternalIpRequest, 1200);
StringCbPrintf(getExternalIpRequest, 1200,
"<?xml version=\"1.0\"?>"
"<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
"<SOAP-ENV:Body>"
"<m:AddPortMapping xmlns:m=\"urn:schemas-upnp-org:service:WANIPConnection:1\">"
"<NewRemoteHost xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"string\">"
""
"</NewRemoteHost>"
"<NewExternalPort xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"ui2\">"
"%s"
"</NewExternalPort>"
"<NewProtocol xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"string\">"
"%s"
"</NewProtocol>"
"<NewInternalPort xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"ui2\">"
"%s"
"</NewInternalPort>"
"<NewInternalClient xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"string\">"
"%s"
"</NewInternalClient>"
"<NewEnabled xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"boolean\">"
"1"
"</NewEnabled>"
"<NewPortMappingDescription xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"string\">"
"%s"
"</NewPortMappingDescription>"
"<NewLeaseDuration xmlns:dt=\"urn:schemas-microsoft-com:datatypes\" dt:dt=\"ui4\">"
"0"
"</NewLeaseDuration>"
"</m:AddPortMapping>"
"</SOAP-ENV:Body>"
"</SOAP-ENV:Envelope>\r\n\r\n", externalPort, protocol, internalPort, internalp, entryDescription);
// externalPort, protocol, internalPort, internalp, entryDescription are **char*** type.
char getExternalIpRequestHeader[1500];
ZeroMemory(getExternalIpRequestHeader, 1500);

StringCbPrintf(getExternalIpRequestHeader, 1500,
"POST /UD/?3 HTTP/1.1\r\n"
"Content-Type: text/xml; charset=\"utf-8\"\r\n"
"SOAPAction: \"urn:schemas-upnp-org:service:WANIPConnection:1#GetExternalIPAddress\"\r\n"
"User-Agent: Mozilla/4.0 (compatible; UPnP/1.0; Windows 9x)\r\n"
"Host: %s\r\n"
"Content-Length: %s\r\n"
"Connection: Keep-Alive\r\n"
"Cache-Control: no-cache\r\n"
"Pragma: no-cache\r\n\r\n", upnpDeviceIp, strlen(getExternalIpRequest));

但是当我在控制台中回显它时,我收到了访问冲突消息。

更新我注意到如果我评论 StringCbPrintf(getExternalIpRequest) 或 StringCbPrintf(getExternalIpRequestHeader) 之一,将不会崩溃。

最佳答案

格式化 getExternalIpRequestHeader 时,Content-Length header 使用 %s,它需要一个 char*值,但是您要为其提供 strlen() 的返回值,这是一个 int,因此您需要使用 %d .

关于c++ - 使用 StringCbPrintf 格式化缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19942268/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com