gpt4 book ai didi

php - c 多部分表单数据从服务器返回错误请求

转载 作者:行者123 更新时间:2023-11-30 17:04:34 24 4
gpt4 key购买 nike

我刚刚创建了 stackoverflow 帐户,因为我在使用 C 应用程序发送多部分/表单数据时遇到严重问题。我可以使用许多库来发送,但出于教育目的,我从头开始做这一切,所以请耐心等待。

问题是,当我使用应用程序发送请求时,服务器恰好返回 400 Bad Request 。我看到了这个类似的问题here on stackoverflow尽管如此,我还是无法解决该请求。下面是我的请求以及从服务器返回的屏幕截图。

screenshot of program when run

// this is the temp_send which is incomplete
// adds the post data later
char *temp_send="POST http://localhost/france/test.php HTTP/1.1\r\n"
"Host: localhost\r\n"
"Accept: image/gif, image/jpeg, */*\r\n"
"Accept-Language: en-us\r\n"
"Content-Type: multipart/form-data; boundary=---------------------------7d41b838504d8\r\n"
"Accept-Encoding: gzip, deflate\r\n"
"User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"
"Connection: keep-alive\r\n"
"Cache-Control: no-cache\r\n\r\n"
"---------------------------7d41b838504d8 Content-Disposition: form-data; name=\"name\"\r\n"
"%s\r\n"
"---------------------------7d41b838504d8--\r\n";

char *postdata="testval";

char *sendbuf=NULL;
sendbuf=(char*)malloc(sizeof(char)*SENDLEN);
sprintf(sendbuf, temp_send, postdata);

注意:“testval”以某种方式在 <pre> 中返回标签,但我需要错误消失。我意识到这是一个广泛的问题,但我只需要缩小多部分/表单数据的发送范围。

注 2:我也知道我可以使用 application/x-www-form-urlencoded我已经使用过它并且它可以工作,但是由于我将进行更复杂的文件上传,所以我需要使用 multipart-form/data我首先尝试使用简单的纯文本来降低复杂性

最佳答案

我终于知道了如何使用 multipart/post-data 来发布简单的数据。我使用了 fiddler 并查看了数据是如何传递的。内容长度恰好是 238,我还没有尝试过其他数据,但将内容长度设置为 238 或更大似乎总是有效。这是现在的代码。

char *sendbuf="POST http://localhost/france/test.php HTTP/1.1\r\n"
"Host: localhost\r\n"
"Content-Length: 238\r\n" // content-length >=238
"Content-Type: multipart/form-data; boundary=----12345\r\n\r\n"
"------12345\r\n"
"Content-Disposition: form-data; name=\"name\"\r\n\r\n"
"This is the first string\r\n"
"------12345\r\n"
"Content-Disposition: form-data; name=\"hell\"\r\n\r\n"
"this is another string\r\n"
"------12345\r\n";

重要提示:发送数据时,Content-Length 似乎很重要,而且边界之后的 - 数量更多。如果边界有 k 条破折号,那么后面的破折号似乎总是 k+2

关于php - c 多部分表单数据从服务器返回错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35708552/

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