为什么在c++中使用http post失败
why fail with http post in c++?
我有下面的代码,但nginx给了我500。我不知道为什么,请帮帮我。
和标题在这里,
我找不到任何错误。有人遇到过吗?
POST http://sss.com/ HTTP/1.1
Host: sss.com
Connection: keep-alive
Content-Length: 241047
Cache-Control: max-age=0
Origin: http://sss.com
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/15.0.849.0 Safari/535.1
Content-Type: multipart/form-data; boundary=----WebKitFormBoundarybZRmyZBq9p09AotO
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Referer: http://sss.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: PHPSESSID=krje6jlnls57ejc0p4jqhkd2k7
------WebKitFormBoundarybZRmyZBq9p09AotO
Content-Disposition: form-data; name="type"
4
------WebKitFormBoundarybZRmyZBq9p09AotO
Content-Disposition: form-data; name="upfile"; filename="/home/heida/test/1152_648.png"
Content-Type: application/octet-stream
------WebKitFormBoundarybZRmyZBq9p09AotO--
nginx给我的结果如下:
HTTP/1.1 500 Internal Server Error Server: nginx Date: Tue, 29 Mar 2016 12:28:23 GMT Content-Type: text/html Content-Length: 537 Connection: close ETag: "54b4def1-219"
代码如下:
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <iostream>
#include <string.h>
#include <netdb.h>
#include <cerrno>
using namespace std;
std::string get_file_contents(const char *filename)
{
std::FILE *fp = std::fopen(filename, "rb");
if (fp)
{
std::string contents;
std::fseek(fp, 0, SEEK_END);
contents.resize(std::ftell(fp));
std::rewind(fp);
std::fread(&contents[0], 1, contents.size(), fp);
std::fclose(fp);
return(contents);
}
throw(errno);
}
int request(char* hostname, char* api,string & content)
{
//socket
struct hostent* host_addr = gethostbyname(hostname);
if (host_addr == NULL)
{
cout<<"Unable to locate host"<<endl;
return -103;
}
sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons((unsigned short)80);
sin.sin_addr= *((struct in_addr *)host_addr->h_addr);
bzero(&(sin.sin_zero),8);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
return -100;
}
if (connect(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr) ) == -1)
{
cout<<"connect failed"<<endl;
return -101;
}
char send_str[2048] = {0};
strcat(send_str, "POST ");
strcat(send_str, api);
strcat(send_str, " HTTP/1.1rn");
strcat(send_str, "Host: ");
strcat(send_str, hostname);
strcat(send_str, "rn");
strcat(send_str, "Connection: keep-alivern");
char content_header[100];
sprintf(content_header,"Content-Length: %drn", content.size());
strcat(send_str, content_header);
strcat(send_str, "Cache-Control: max-age=0rn");
strcat(send_str, "Origin: http://yyfs.yy.comrn");
strcat(send_str, "Upgrade-Insecure-Requests: 1rn");
strcat(send_str, "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/15.0.849.0 Safari/535.1rn");
strcat(send_str, "Content-Type: multipart/form-data; boundary=----WebKitFormBoundarybZRmyZBq9p09AotOrn");
strcat(send_str, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8rn");
strcat(send_str, "Referer: http://yyfs.yy.com/rn");
strcat(send_str, "Accept-Encoding: gzip,deflate,sdchrn");
strcat(send_str, "Accept-Language: zh-CN,zh;q=0.8rn");
strcat(send_str,"Cookie: PHPSESSID=krje6jlnls57ejc0p4jqhkd2k7rn");
strcat(send_str, "rn");
strcat(send_str,"------WebKitFormBoundarybZRmyZBq9p09AotOrn");
strcat(send_str,"Content-Disposition: form-data; name="type"rnrn");
strcat(send_str, "rn");
strcat(send_str, "4rn");
strcat(send_str,"------WebKitFormBoundarybZRmyZBq9p09AotOrn");
strcat(send_str,"Content-Disposition: form-data; name="upfile"; filename="/home/heida/test/1152_648.png"rn");
strcat(send_str,"Content-Type: application/octet-streamrnrn");
cout<<send_str<<endl;
if (send(sock, send_str, strlen(send_str),0) == -1)
{
cout<<"send failed"<<endl;
return -101;
}
if (send(sock, content.c_str(), content.size(),0) == -1)
{
cout<<"send failed"<<endl;
return -1;
}
char ends[190] = {0};
strcat(ends, "rn------WebKitFormBoundarybZRmyZBq9p09AotO--rn");
cout<<ends<<endl;
if (send(sock, ends, strlen(ends),0) == -1)
{
cout<<"send failed"<<endl;
return -1;
}
char recv_str[4096] = {0};
if (recv(sock, recv_str, sizeof(recv_str), 0) == -1)
{
cout<<"recv failed"<<endl;
return -101;
}
cout<<recv_str<<endl;
return 0;
}
int main()
{
std::string files = get_file_contents("1152_648.png");
cout<<files.size()<<endl;
request("sss.com","http://ssy.com/",files);
return 0;
}
我找错地方了,是内容长度。内容长度包括文件大小和低于大小的
------WebKitFormBoundaryZRmyZBq9p09AotO内容处置:表单数据;name="file";filename="/home/heida/test/1152_648.png"内容类型:应用程序/八位字节流
塒NG
------WebKitFormBoundaryZRmyZBq9p09AotO-
右侧代码:
char endsss[190] = {0};
strcat(endsss,"------WebKitFormBoundarybZRmyZBq9p09AotOrn");
strcat(endsss,"Content-Disposition: form-data; name="file"; filename="/home/heida/test/1152_648.png"rn");
strcat(endsss,"Content-Type: application/octet-streamrnrn");
char ends[190] = {0};
strcat(ends, "rn------WebKitFormBoundarybZRmyZBq9p09AotO--rn");
char content_header[100];
sprintf(content_header,"Content-Length: %drn",content.size()+strlen(endsss)+strlen(ends));
相关文章:
- 如何使用boost/beast从HTTP POST请求中解析和提取有效负载?
- C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据)
- HTTP POST 请求不允许我定义上下文类型
- cpp libcurl 在 HTTP post 调用中发送 zip 文件,而不使用 multipart\form-da
- 使用 http post 在 FCM 中推送通知
- 带有边界获取错误的 HTTP POST
- HTTP post 使用 libcurl 创建 InfluxDB 数据库
- C HTTP POST 400不良请求
- 处理HTTP POST请求/响应
- 使用 cpprest 通过 json 对象在 http post 中发送图像
- 如何使用libmicrohttpd/microhttpd创建HTTP POST请求
- 使用libCURL的简单HTTP Post
- 为什么在c++中使用http post失败
- 将 JSON 对象返回到 HTTP POST REST 调用C++
- 如何向包含多部分 HTTP POST 的 SendGrid Web API 发出请求
- HTTP POST 请求使用 boost::asio
- mosyn Http post to php using AMDownload
- 将 http "Post" 请求发送到 C 或 C++ 中的 php 代码
- gSOAP - 修改 HTTP POST 标头
- 发送http post消息错误报告的安全方式