c httpclient抓取网页(想自己写个客户端,模拟浏览器向服务器(如lighttpd等)上传数据 )
优采云 发布时间: 2021-09-24 17:04c httpclient抓取网页(想自己写个客户端,模拟浏览器向服务器(如lighttpd等)上传数据
)
我想编写自己的客户端并模拟浏览器将数据上传到服务器(如lighttpd)。用httprequest获取数据包。以下是发送的数据内容:
引述
POST/upload.php HTTP/1.0
接受:image/gif、image/x-xbitmap、image/jpeg、image/pjpeg、application/vnd.ms-excel、application/msword、application/vnd.ms-powerpoint、*/*
接受语言:en us
接受编码:gzip,deflate
用户代理:Mozilla/4.0
内容长度:180
主持人:19人(k23)16人(k24)(k21)180人)
内容类型:application/x-www-form-urlencoded
-----------------------------7d93a924b05a2
内容配置:表单数据;;filename=“C:\tmp\hello.txt”
内容类型:文本/纯文本
实现时,代码大致如下:
#include
#include
#include
#include
#include
// post a big file
#define Req "POST /upload.php HTTP/1.0\r\n" \
"Accept:image/gif, image/x-xbitmap, image/jpeg, image/pjepg, application/vnd.ms-excel, application/msword, applicationvnd.ms-powerpoint, */*\r\n" \
"Accept-Language:en-us\r\n" \
"Accept-Encoding:gzip, deflate\r\n" \
"User-Agent:Mozilla/4.0\r\n" \
"Host:192.168.1.180\r\n" \
"Content-Type:application/x-www-urlencoded\r\n" \
"-----------------------------7d91a515b05a2\r\n" \
"Content-Disposition:form-data;name=\"upload_file\";filename=\"tmp.gz\"\r\n" \
"Content-Type:application/x-gzip\r\n"
#define DST_IP "192.168.1.180"
#define ReqLen sizeof(Req)
int main()
{
struct sockaddr_in srv;
int sock, nbytes;
char sndbuf[1024] = {0};
char recbuf[1024] = {0};
if((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
fprintf(stderr, "socket() error!\n");
exit(1);
}
srv.sin_family = AF_INET;
srv.sin_addr.s_addr = inet_addr(DST_IP);
srv.sin_port = htons(80);
if((connect(sock, (struct sockaddr *)&srv, sizeof(struct sockaddr))) == -1)
{
printf("connect() error!\n");
exit(1);
}
strncpy(sndbuf, Req, ReqLen);
if(write(sock, sndbuf, ReqLen) == -1)
{
fprintf(stderr, "write() error!\n");
exit(1);
}
// get response
int bytes;
while(1)
{
nbytes = read(sock, recbuf, 1023);
if(nbytes < 0)
break;
recbuf[nbytes] = '\0';
printf(recbuf);
}
close(sock);
return 0;
}