gpt4 book ai didi

c - 使用套接字在 C 中发送发布数据时出错

转载 作者:行者123 更新时间:2023-11-30 15:03:39 25 4
gpt4 key购买 nike

我想将帖子数据发送到 php 脚本,但 php 从未获取我的帖子数据。这是我的代码:

    #include<stdio.h>
#include<winsock2.h>
#include <string.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char message[1024] , server_reply[2000];
int recv_size;

printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}

printf("Initialised.\n");

//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}

printf("Socket created.\n");


server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );

//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}

puts("Connected");

// Work well with Get Method
// strcat(message, "GET /voleur/receiver.php?arg1=Hello HTTP/1.1\n");
strcpy(message, "POST /voleur/receiver.php HTTP/1.1\r\n");
strcat(message, "Host: 127.0.0.1:80\r\n");
strcat(message, "Content-Type: application/x-www-form-urlencoded\r\n");
strcat(message, "Content-Length: 10\r\n");
strcat(message, "Accept-Charset: utf-8\r\n\r\n");
strcat(message, "arg1=Hello");




if( send(s , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}

puts("Data Send\n");

//Receive a reply from the server
if((recv_size = recv(s , server_reply , 2000 , 0)) == SOCKET_ERROR)
{
puts("recv failed");
}

puts("Reply received\n");

//Add a NULL terminating character to make it a proper string before printing
server_reply[recv_size] = '\0';
puts(server_reply);

return 0;
}

这是 php 脚本:

<?php
echo "POST DATA : ".json_encode($_POST);
?>

这里是回复:

Reply received

HTTP/1.1 200 OK
Date: Fri, 18 Nov 2016 10:34:21 GMT
Server: Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.8
X-Powered-By: PHP/7.0.8
Content-Length: 14
Content-Type: text/html; charset=UTF-8

POST DATA : []

当我尝试通过 GET 方法发送数据时效果很好,但对于 POST 方法,PHP 脚本不会从客户端接收任何数据。

最佳答案

谢谢 David Schwartz,这是正确的代码:

#include<stdio.h>
#include<winsock2.h>
#include <string.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

int main(int argc , char *argv[])
{
WSADATA wsa;
SOCKET s;
struct sockaddr_in server;
char message[1024] , server_reply[2000];
int recv_size;

printf("\nInitialising Winsock...");
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
{
printf("Failed. Error Code : %d",WSAGetLastError());
return 1;
}

printf("Initialised.\n");

//Create a socket
if((s = socket(AF_INET , SOCK_STREAM , 0 )) == INVALID_SOCKET)
{
printf("Could not create socket : %d" , WSAGetLastError());
}

printf("Socket created.\n");


server.sin_addr.s_addr = inet_addr("127.0.0.1");
server.sin_family = AF_INET;
server.sin_port = htons( 80 );

//Connect to remote server
if (connect(s , (struct sockaddr *)&server , sizeof(server)) < 0)
{
puts("connect error");
return 1;
}

puts("Connected");

// Work well with Get Method
// strcat(message, "GET /voleur/receiver.php?arg1=Hello HTTP/1.1\n");
strcpy(message, "POST /voleur/receiver.php HTTP/1.1\r\n");
strcat(message, "Host: 127.0.0.1:80\r\n");
strcat(message, "Content-Type: application/x-www-form-urlencoded\r\n");
strcat(message, "Content-Length: 16\r\n");
strcat(message, "Accept-Charset: utf-8\r\n\r\n");
strcat(message, "arg1=Hello World");




if( send(s , message , strlen(message) , 0) < 0)
{
puts("Send failed");
return 1;
}

puts("Data Send\n");

//Receive a reply from the server
if((recv_size = recv(s , server_reply , 2000 , 0)) == SOCKET_ERROR)
{
puts("recv failed");
}

puts("Reply received\n");

//Add a NULL terminating character to make it a proper string before printing
server_reply[recv_size] = '\0';
puts(server_reply);

return 0;
}

关于c - 使用套接字在 C 中发送发布数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40674979/

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