gpt4 book ai didi

C++ 使用 GET 发送数据

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

我有这个代码,但我有这个问题:错误 1 ​​error C2110: '+' : 无法添加两个指针2 IntelliSense:表达式必须具有整数或无作用域的枚举类型

我正在尝试使用 m_AccountID 变量数据将数据获取到 test.php 文件,我尝试了很多方法,但我遇到了这两个错误。

有解决办法吗?:)

void ConnectEx::SendDataPHP(){
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
//cout << "WSAStartup failed.\n";
//system("pause");
return;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.mysite.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
//cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
//cout << "Could not connect";
//system("pause");
return;
}

//cout << "Connected.\n";

send(Socket, "GET /api/test.php?username=" +m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n", strlen("GET /api/test.php?username="+m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
//cout << buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
//system("pause");
return;

}

class ConnectEx
{
public:
void SendDataPHP();
.
.
.
.
char m_AccountID[11];
.
.
.


}; extern ConnectEx gConnectEx;

最佳答案

只需将它连接到字符串:

send( Socket ,
std::string( "GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n" ).c_str( ) ,
strlen("GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n") , 0);

Demo

此外,如果您之后只是重复该字符串,请先将其保存到一个变量中,以便您可以重复使用它。而且我不建议使用 std::strlen因为它很旧。

宁愿这样做:

std::string m_AccountID;
std::string data;


m_AccountID = "12345";
data = "GET /api/test.php?username=" + m_AccountID + " HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n"

send( Socket , data.c_str( ) , data.size( ) , 0 );

char m_AccountID[11];应该是 std::string m_AccountID;

您的代码现在应该如下所示:

void ConnectEx::SendDataPHP(){
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
//cout << "WSAStartup failed.\n";
//system("pause");
return;
}
SOCKET Socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct hostent *host;
host = gethostbyname("www.mysite.com");
SOCKADDR_IN SockAddr;
SockAddr.sin_port = htons(80);
SockAddr.sin_family = AF_INET;
SockAddr.sin_addr.s_addr = *((unsigned long*)host->h_addr);
//cout << "Connecting...\n";
if (connect(Socket, (SOCKADDR*)(&SockAddr), sizeof(SockAddr)) != 0){
//cout << "Could not connect";
//system("pause");
return;
}

//cout << "Connected.\n";

send(Socket, std::string( "GET /api/test.php?username=" +m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n" ).c_str( ) , strlen("GET /api/test.php?username="+m_AccountID+ "HTTP/1.1\r\nHost: www.mysite.com\r\nConnection: close\r\n\r\n"), 0);
char buffer[10000];
int nDataLength;
while ((nDataLength = recv(Socket, buffer, 10000, 0)) > 0){
int i = 0;
while (buffer[i] >= 32 || buffer[i] == '\n' || buffer[i] == '\r') {
//cout << buffer[i];
i += 1;
}
}
closesocket(Socket);
WSACleanup();
//system("pause");
return;

}

class ConnectEx
{
public:
void SendDataPHP();
.
.
.
.
std::string m_AccountID;
.
.
.


}; extern ConnectEx gConnectEx;

确保你也#include <string>

关于C++ 使用 GET 发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38165715/

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