C ++使用GET

时间:2016-07-03 00:39:29

标签: c++ http

我有这个代码,我有这个问题: 错误1错误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;

2 个答案:

答案 0 :(得分:1)

只需将其连接到字符串:

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>

答案 1 :(得分:0)

现在我做到了,我用strcat来合并变量,而不是把它放到send和它的工作中。谢谢你的帮助!