gpt4 book ai didi

php - HTTP POST 未在 c 中返回正确的响应

转载 作者:可可西里 更新时间:2023-11-01 16:36:58 25 4
gpt4 key购买 nike

我已经用 C 语言编写了一个 http 客户端,并为我的服务器创建了一个 post 方法,该方法正在运行一个 php 脚本,该脚本将接收客户端参数并将一些响应打印回客户端。但是,服务器在成功连接套接字后不返回任何内容。我的代码中缺少什么?

这是我基于 off winsock2 的 c 客户端

    /*
* Create a TCP socket
* @author Salim Said
* Jan 7 2016
*/

#include<stdio.h>
#include<winsock2.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 , 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("196.46.123.151");
server.sin_addr.s_addr = inet_addr("66.96.147.96");
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");


//----------------------------------------------------------HTTP HEADER PLUS MESSAGE--------------------------------------//
message = "POST /echo.php HTTP/1.0\r\n Host: www.vocha.co.tz\r\n Content-type: application/x-www-form-urlencoded\r\n Content-Length: 16\r\n\r\n name=iAmWorking!";

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';
//print the Server reply
puts(server_reply);
//show receive success message
puts("Data received successfully!");
//We are done receiving data! close the socket
closesocket(s);
WSACleanup();
return 0;
}

这是我的服务器端 php 脚本

    <?php

// get name from post parameter and store it in variable $name
$name=$_POST['name'];

//echo back a greeting with the user's name
//this is what will appear in client's HTTP response
echo 'Hi ' . $name;

?>

这是服务器响应的一部分和我的调试消息。

    Initialising Winsock...
Initialised.
Socket created.
Connected
Data Send
Reply received
<*****Missing response is supposed to appear here*****>
Data received successfully!

最佳答案

您的消息有错误的 HTTP 语法格式,被服务器拒绝且没有响应。

如果你拿这个:

message = "POST /echo.php HTTP/1.0\r\n Host: www.vocha.co.tz\r\n Content-type: application/x-www-form-urlencoded\r\n Content-Length: 16\r\n\r\n name=iAmWorking!";

意思是:

POST /echo.php HTTP/1.0[CR][LF]
[SP]Host: www.vocha.co.tz[CR][LF]
[SP]Content-type: application/x-www-form-urlencoded[CR][LF]
[SP]Content-Length: 16[CR][LF]
[CR][LF]
name=iAmWorking!

看到了吗?你一直在 Header 标记前添加一个空格(“\r\n”而不是“\r\n”),这是一个无效的语法,服务器更喜欢静默关闭 tcp/ip 连接,而不是花时间响应一个攻击者脚本(因为这个可以被 HTTP smuggling 脚本使用)。

关于php - HTTP POST 未在 c 中返回正确的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34651692/

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