HTTP POST没有返回正确的响应c

时间:2016-01-07 09:36:11

标签: php c http winsock2

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

这是我的基于winsock2的客户端

    /*
    *   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!

1 个答案:

答案 0 :(得分:1)

您的邮件的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脚本可以使用此脚本)。