发送HTTP"邮政"请求要在C或C PHP的code ++ [英] Send Http "Post" Request To Php Code in C or C++

查看:173
本文介绍了发送HTTP"邮政"请求要在C或C PHP的code ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图发送一个后reguest到我的PHP文件并获得信息回来,它工作正常,但

I'm trying to send a "post" reguest to my php file and get the info back, it works fine, but

也打印从PHP文件我的回应之前打印的东西。这是它打印

it also print something before printing my response from the php file. this is what it print

第一:


HTTP/1.1 200 OK
Date: Fri, 20 Apr 2012 10:19:12 GMT
Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.6
X-Powered-By: PHP/5.3.6
Content-Length: 12
Connection: close
Content-Type: text/html

然后我的回答:


hello world

我怎么只能打印什么,我从myphp code获得,无需:

how can i only print what i'm getting from myphp code, without:


HTTP/1.1 200 OK
Date: Fri, 20 Apr 2012 10:19:12 GMT
Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.6
X-Powered-By: PHP/5.3.6
Content-Length: 12
Connection: close
Content-Type: text/html

在code我使用的是:

the code i'm using is:

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>

#define SA      struct sockaddr
#define MAXLINE 4096
#define MAXSUB  200


#define LISTENQ         1024

extern int h_errno;


ssize_t process_http(int sockfd, char *host, char *page, char *poststr)
{
     char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
    ssize_t n;
    snprintf(sendline, MAXSUB,
             "POST %s HTTP/1.0\r\n"
             "Host: %s\r\n"
             "Content-type: application/x-www-form-urlencoded\r\n"
             "Content-length: %d\r\n\r\n"
             "%s", page, host, strlen(poststr), poststr);

    write(sockfd, sendline, strlen(sendline));
    while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
        recvline[n] = '\0';
        printf("%s", recvline);  // <-- this
    }
    return n;

}





int main(void)
{



    int sockfd;
    struct sockaddr_in servaddr;

    char **pptr;
    //********** You can change. Puy any values here *******
    char *hname = "localhost";
    char *page = "/mysql_update.php";
    char *poststr = "server=dd sd sdsdt\n";
    //*******************************************************

    char str[50];
    struct hostent *hptr;
    if ((hptr = gethostbyname(hname)) == NULL) {
        fprintf(stderr, " Server down error for host: %s: %s",
                hname, hstrerror(h_errno));
        exit(1);
    }
    printf("hostname: %s \n", hptr->h_name);
    if (hptr->h_addrtype == AF_INET
        && (pptr = hptr->h_addr_list) != NULL) {
        printf("address: %s\n",
               inet_ntop(hptr->h_addrtype, *pptr, str,
                         sizeof(str)));
    } else {
        fprintf(stderr, "Error call inet_ntop \n");
    }

    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(80);
    inet_pton(AF_INET, str, &servaddr.sin_addr);

    connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
    process_http(sockfd, hname, page, poststr);
    close(sockfd);
    exit(0);


}

请帮我解决这个问题,一步步让我能理解它,给我信息,并请,如果你能给我一个例子。 (这将帮助我和其他人)

Please help me to fix this problem, step by step so' i can understand it, give me information and please if you can' show me a example. ( this will help me and others )

推荐答案

您收到的是由HTTP版本,服务器响应状态code和各种HTTP响应头响应的第一部分。

First part of the response you received is made up of HTTP version, Server Response Status Code and various HTTP response headers.

HTTP / 1.1 200 OK日期:星期五,2012年4月20日十时十九分12秒格林尼治标准​​时间服务器:
  阿帕奇/ 2.2.21(Unix的)了mod_ssl / 2.2.21的OpenSSL / 0.9.8r DAV / 2 PHP / 5.3.6
  的X已启动方式:PHP / 5.3.6的Content-Length:12连接:关闭
  内容类型:text / html的

HTTP/1.1 200 OK Date: Fri, 20 Apr 2012 10:19:12 GMT Server: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.6 X-Powered-By: PHP/5.3.6 Content-Length: 12 Connection: close Content-Type: text/html

在头遵循HTTP响应体(你需要提取):

After headers follows HTTP response body (which you need to extract):

的Hello World

hello world

HTTP头和身体与字符的顺序如下分隔: \\ r \\ n \\ r \\ n 。因此,所有你需要做的是寻找并提取一切,是它背后。

HTTP headers and body are separated with the following sequence of characters: \r\n\r\n. So all you need to do is to search for it and extract everything that is behind it.

您可以自己做(套接字,解析...),但我的建议是使用一些HTTP库:的WinInet WINHTTP (都是微软的)或的libcurl (开源)。

You can do this yourself (sockets, parsing...) but my advice is to use some of HTTP libraries: WinInet, WinHttp (both are Microsoft's) or libCurl (open source).

这篇关于发送HTTP&QUOT;邮政&QUOT;请求要在C或C PHP的code ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆