我正在尝试向我的php文件发送“post”reguest并获取信息,它工作正常,但
它还会在从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);
}
请帮我解决这个问题,一步一步这样'我能理解它,给我信息,如果你能给我一个例子,请告诉我。 (这将有助于我和其他人)
答案 0 :(得分:6)
您收到的响应的第一部分由HTTP版本,服务器响应状态代码和各种HTTP响应标头组成。
HTTP / 1.1 200 OK日期:星期五,2012年4月20日10:19:12 GMT服务器: 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内容长度:12连接:关闭 内容类型:text / html
标题跟随HTTP响应主体(您需要提取)之后:
你好世界
HTTP标头和正文用以下字符序列分隔:\r\n\r\n
。所以你需要做的就是搜索它并提取其背后的所有内容。
你可以自己做(套接字,解析......),但我的建议是使用一些HTTP库:WinInet
,WinHttp
(都是微软的)或libCurl
(开源)。
答案 1 :(得分:1)
响应标题位与内容之间用空行分隔。
需要考虑不同的行结尾。基本上忽略\r
s。
所以第一次读取行,直到你得到一个空白然后开始打印它们!
修改强>
您的回复如下
HTTP/1.1 200 OK
Date: Fri, 20 Apr 2012 10:19:12 GMT
...
Content-Type: text/html
Hello World
请注意,HTTP标头和响应之间存在空行(在本例中为HTML)。