gpt4 book ai didi

c++ - 如何通过 post 方法将数据发送到 C++ 中的 php 文件?

转载 作者:行者123 更新时间:2023-11-30 04:33:41 25 4
gpt4 key购买 nike

我是 C++ 的新手。最近我试图通过 post 方法将一些数据传递给 c++ 中的 php 文件。我试过来自 MSDN send Function 的教程.代码如下:

#ifndef UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN

#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
#include <conio.h>

// Link with ws2_32.lib
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT 27015

int main() {

//----------------------
// Declare and initialize variables.
int iResult;
WSADATA wsaData;
struct hostent *host;

SOCKET ConnectSocket = INVALID_SOCKET;
struct sockaddr_in clientService;

int recvbuflen = DEFAULT_BUFLEN;
char *sendbuf = "Client: sending data test";
char recvbuf[DEFAULT_BUFLEN] = "";

//----------------------
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != NO_ERROR) {
wprintf(L"WSAStartup failed with error: %d\n", iResult);
return 1;
}

//----------------------
// Create a SOCKET for connecting to server
ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (ConnectSocket == INVALID_SOCKET) {
wprintf(L"socket failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}

//----------------------
// The sockaddr_in structure specifies the address family,
// IP address, and port of the server to be connected to.
clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );

clientService.sin_port = htons( 80 );

//----------------------
// Connect to server.
iResult = connect( ConnectSocket, (SOCKADDR*) &clientService, sizeof(clientService) );
if (iResult == SOCKET_ERROR) {
wprintf(L"connect failed with error: %d\n", WSAGetLastError() );
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

//----------------------
// Send an initial buffer
iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
if (iResult == SOCKET_ERROR) {
wprintf(L"send failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

printf("Bytes Sent: %d\n", iResult);

// shutdown the connection since no more data will be sent
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
wprintf(L"shutdown failed with error: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

// Receive until the peer closes the connection
do {

iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
if ( iResult > 0 )
wprintf(L"Bytes received: %d\n", iResult);
else if ( iResult == 0 )
wprintf(L"Connection closed\n");
else
wprintf(L"recv failed with error: %d\n", WSAGetLastError());

} while( iResult > 0 );


// close the socket
iResult = closesocket(ConnectSocket);
if (iResult == SOCKET_ERROR) {
wprintf(L"close failed with error: %d\n", WSAGetLastError());
WSACleanup();
return 1;
}

WSACleanup();
getch();
}

它将数据发送到 localhost 的主页,但我想将数据发送到 localhost 中的其他地方(例如,像“localhost/test/index.php”)。如果我将代码从 clientService.sin_addr.s_addr = inet_addr( "127.0.0.1"); 更改为 clientService.sin_addr.s_addr = inet_addr( "127.0.0.1/test/index.php "); 然后我在 exe 文件中收到一些错误。但是程序编译成功。

请给我一个解决方案。我现在该怎么办?

最佳答案

您采用了错误的方法。低级套接字层只允许您连接到特定服务器(例如,您在 127.0.0.1 上运行的本地 http 服务器监听端口 80)并发送原始数据。您需要与该服务器进行 HTTP 通信才能真正执行您想要执行的操作。

我建议,如果您是 C++ 的新手,您可以查看许多 C++ 第三方库中的一个,它允许您与 http 服务器“进行 HTTP 对话”- 例如,libCurl

关于c++ - 如何通过 post 方法将数据发送到 C++ 中的 php 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6595138/

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