[英]Send Http “Post” Request To Php Code in C or C++
I'm trying to send a "post" reguest to my php file and get the info back, it works fine, but 我正在尝试向我的php文件发送一个“ post”请求,并获取信息,它工作正常,但是
it also print something before printing my response from the php file. 在打印来自php文件的响应之前,它还会打印一些内容。 this is what it print
这是它打印的
first: 第一:
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
and then my response: 然后我的回应:
hello world
how can i only print what i'm getting from myphp code, without: 我如何只打印我从myphp代码中得到的内容,而没有:
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
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 )
(这将帮助我和其他人)
First part of the response you received is made up of HTTP version, Server Response Status Code and various HTTP response headers. 您收到的响应的第一部分由HTTP版本,服务器响应状态代码和各种HTTP响应标头组成。
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 / 1.1 200 OK日期:2012年4月20日星期五,GMT服务器:Apache / 2.2.21(Unix)mod_ssl / 2.2.21 OpenSSL / 0.9.8r DAV / 2 PHP / 5.3.6 X-Powered-提供者:PHP / 5.3.6内容长度:12连接:关闭内容类型:text / html
After headers follows HTTP response body (which you need to extract): 标头跟随HTTP响应正文(您需要提取)之后:
hello world
你好,世界
HTTP headers and body are separated with the following sequence of characters: \\r\\n\\r\\n
. HTTP标头和正文由以下字符序列分隔:
\\r\\n\\r\\n
。 So all you need to do is to search for it and extract everything that is behind it. 因此,您要做的就是搜索它并提取其背后的所有内容。
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库:
WinInet
, WinHttp
(均为Microsoft的)或libCurl
(开源)。
The responses header bit is separated from the content by a blank line. 响应头位与内容之间用空行分隔。
Need to take into account different line endings. 需要考虑不同的行尾。 Basically ignore
\\r
s. 基本上忽略
\\r
s。
So 1st read lines until you get a blank one then start printing them! 因此,请先读取第一行,直到获得空白,然后再开始打印!
EDIT 编辑
You have a response as follows 您有以下回应
HTTP/1.1 200 OK
Date: Fri, 20 Apr 2012 10:19:12 GMT
...
Content-Type: text/html
Hello World
Notice that there is a blank line between the HTTP headers and the response (HTML in this case). 请注意,HTTP标头和响应(在本例中为HTML)之间有一个空白行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.