gpt4 book ai didi

C - 尝试发送()数据但有错误

转载 作者:行者123 更新时间:2023-11-30 15:27:18 25 4
gpt4 key购买 nike

我正在尝试使用 GET 请求从文件中逐行发送数据(换行符已删除)。问题是,当我检查我的 apache 访问日志时,即使我对它进行 url 编码(也是 2x 编码),也不会发送带有空格分隔的三个单词的请求。

文本文件示例:

asd

asd asd

asd asd asd <- not sent

代码:

asprintf(&httpget, 
"GET /%s HTTP/1.1\r\n"
"Host: 127.0.0.1\r\n"
"User-Agent: \r\n"
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,;q=0.5\r\n"
"Accept-Language: en-us,en;q=0.5\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
"Cache-Control: max-age=\r\n"
"\r\n", line_buffer);

send(socket_handle, httpget, strlen(httpget), 0);

请求应该有效,因为我尝试使用其他脚本复制请求。

所以我发现了一些事情......

当您发送a时,它会执行请求,但之后不会发送另一行。但我仍然不认为这是网络服务器或无效 header 的问题,因为例如在 BurpSuite 中我可以毫无问题地发送这些 url 未编码数据,并且日志看起来像

127.0.0.1 - - [22/Nov/2014:13:37:04 +0100] "GET /test test test HTTP/1.1" 200 54

完整代码:

  socket_handle = socket(AF_INET, SOCK_STREAM, 0) ;
socket_detials.sin_family = AF_INET ;
socket_detials.sin_addr.s_addr = inet_addr(host);
socket_detials.sin_port = htons(80);
input_buffer = malloc(20000);
line_number = 0;
while (fgets(line_buffer, sizeof(line_buffer), infile)) {
++line_number;
strtok(line_buffer, "\n");
printf("\n\n%s\n\n", line_buffer);
asprintf(&httpget,
"GET /%s HTTP/1.1\r\n"
"Host: 127.0.0.1\r\n"
"User-Agent: \r\n"
"Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,;q=0.5\r\n"
"Accept-Language: en-us,en;q=0.5\r\n"
"Accept-Encoding: gzip,deflate\r\n"
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
"Cache-Control: max-age=\r\n"
"\r\n", line_buffer);
bzero(&(socket_detials.sin_zero), 8);
connect(socket_handle,(struct sockaddr*)&socket_detials, sizeof(struct sockaddr));
if(send(socket_handle, httpget, strlen(httpget), 0) < 0) {
printf("\n\n\n ERROR send() \n\n\n");
}
int recieved = recv(socket_handle, input_buffer, 20000, 0);
if(recieved != 0) {
printf ("\rRecieved %d bytes | Requests made: %d ", recieved, req++);
printf("\n%s\n", httpget);
printf("%s%s\n%s\n", gray, input_buffer, normal);
} else {
printf ("\rRecieved %d bytes | Requests made: %d ", recieved, req++);
printf("\n%sRequest not sent.%s \n\nResponse:\n\n", red, normal);
printf("\n%s\n", httpget);
printf("%s%s\n%s\n", gray, input_buffer, normal);
}
}

为什么在可能无效的请求之后套接字会关闭?

最佳答案

您应该在 HTTP RFC 中查找 URL 编码。空格需要编码为“%20”,但还有许多其他字符也需要编码。与将文本文件发布到服务器相比,这看起来效率极低。

关于C - 尝试发送()数据但有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27077267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com