dongzhanlian6289 2016-02-29 20:02
浏览 51

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

I just made stackoverflow account because I'm having serious troubles sending multipart/form-data using my C application. There are many libraries I could use to send but for educational purposes I am doing it all from scratch so please bear with me.

The issue is that when I send the request using my application, the server happens to return 400 Bad Request. I saw this similar question here on stackoverflow and I am unable to fix the request nonetheless. Below is the request I have and a screenshot of what is returned from the server.

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
"
                "Host: localhost
"
                "Accept: image/gif, image/jpeg, */*
"
                "Accept-Language: en-us
"
                "Content-Type: multipart/form-data; boundary=---------------------------7d41b838504d8
"
                "Accept-Encoding: gzip, deflate
"
                "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
"
                "Connection: keep-alive
"
                "Cache-Control: no-cache

"
                "---------------------------7d41b838504d8 Content-Disposition: form-data; name=\"name\"
"
                "%s
"
                "---------------------------7d41b838504d8--
";

char *postdata="testval";

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

NOTE: Somehow the "testval" is returned in the <pre> tags but I need the error to go away. I realize this is a broad question but I just need to narrow down the sending of multipart/form-data.

NOTE 2: I also know that I can use application/x-www-form-urlencoded and I have already used it and it works but since I will be doing file uploads which are much more complex, I need to use multipart-form/data and I'm trying with simple plain text first to reduce complexity

  • 写回答

1条回答 默认 最新

  • dongsigan2044 2016-02-29 21:52
    关注

    I finally got how to post simple data using multipart/post-data. I used fiddler and saw how the data is being passed. The content-length happened to be 238 and I've not tried with other data yet but setting the content-length to 238 or greater seemed to always work. This is the code now.

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

    IMPORTANT: The Content-Length seems to be important when sending data and also the number of - are more after the boundary.ie. if boundary has k dashes then the following dashes seem to ALWAYS be k+2

    评论

报告相同问题?

悬赏问题

  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭