[英]Send Http “Post” Request To Php Code in C or C++
我正在嘗試向我的php文件發送一個“ post”請求,並獲取信息,它工作正常,但是
在打印來自php文件的響應之前,它還會打印一些內容。 這是它打印的
第一:
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代碼中得到的內容,而沒有:
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
我使用的代碼是:
#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);
}
請幫助我逐步解決此問題,以便“我可以理解它,向我提供信息,請告訴我一個例子。” (這將幫助我和其他人)
您收到的響應的第一部分由HTTP版本,服務器響應狀態代碼和各種HTTP響應標頭組成。
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
標頭跟隨HTTP響應正文(您需要提取)之后:
你好,世界
HTTP標頭和正文由以下字符序列分隔: \\r\\n\\r\\n
。 因此,您要做的就是搜索它並提取其背后的所有內容。
您可以自己執行此操作(套接字,解析...),但是我的建議是使用一些HTTP庫: WinInet
, WinHttp
(均為Microsoft的)或libCurl
(開源)。
響應頭位與內容之間用空行分隔。
需要考慮不同的行尾。 基本上忽略\\r
s。
因此,請先讀取第一行,直到獲得空白,然后再開始打印!
編輯
您有以下回應
HTTP/1.1 200 OK
Date: Fri, 20 Apr 2012 10:19:12 GMT
...
Content-Type: text/html
Hello World
請注意,HTTP標頭和響應(在本例中為HTML)之間有一個空白行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.