我的代码如下,但是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.1\r\n");
strcat(send_str, "Host: ");
strcat(send_str, hostname);
strcat(send_str, "\r\n");
strcat(send_str, "Connection: keep-alive\r\n");
char content_header[100];
sprintf(content_header,"Content-Length: %d\r\n", content.size());
strcat(send_str, content_header);
strcat(send_str, "Cache-Control: max-age=0\r\n");
strcat(send_str, "Origin: http://yyfs.yy.com\r\n");
strcat(send_str, "Upgrade-Insecure-Requests: 1\r\n");
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.1\r\n");
strcat(send_str, "Content-Type: multipart/form-data; boundary=----WebKitFormBoundarybZRmyZBq9p09AotO\r\n");
strcat(send_str, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n");
strcat(send_str, "Referer: http://yyfs.yy.com/\r\n");
strcat(send_str, "Accept-Encoding: gzip,deflate,sdch\r\n");
strcat(send_str, "Accept-Language: zh-CN,zh;q=0.8\r\n");
strcat(send_str,"Cookie: PHPSESSID=krje6jlnls57ejc0p4jqhkd2k7\r\n");
strcat(send_str, "\r\n");
strcat(send_str,"------WebKitFormBoundarybZRmyZBq9p09AotO\r\n");
strcat(send_str,"Content-Disposition: form-data; name=\"type\"\r\n\r\n");
strcat(send_str, "\r\n");
strcat(send_str, "4\r\n");
strcat(send_str,"------WebKitFormBoundarybZRmyZBq9p09AotO\r\n");
strcat(send_str,"Content-Disposition: form-data; name=\"upfile\"; filename=\"/home/heida/test/1152_648.png\"\r\n");
strcat(send_str,"Content-Type: application/octet-stream\r\n\r\n");
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, "\r\n------WebKitFormBoundarybZRmyZBq9p09AotO--\r\n");
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;
}
答案 0 :(得分:0)
我找错了地方,这是内容长度。 内容长度包括文件大小和小于
------ WebKitFormBoundarybZRmyZBq9p09AotO 内容处理:表格数据;命名=&#34;文件&#34 ;;文件名=&#34; /home/heida/test/1152_648.png" Content-Type:application / octet-stream
埘NG
------ WebKitFormBoundarybZRmyZBq9p09AotO -
正确的代码:
char endsss[190] = {0};
strcat(endsss,"------WebKitFormBoundarybZRmyZBq9p09AotO\r\n");
strcat(endsss,"Content-Disposition: form-data; name=\"file\"; filename=\"/home/heida/test/1152_648.png\"\r\n");
strcat(endsss,"Content-Type: application/octet-stream\r\n\r\n");
char ends[190] = {0};
strcat(ends, "\r\n------WebKitFormBoundarybZRmyZBq9p09AotO--\r\n");
char content_header[100];
sprintf(content_header,"Content-Length: %d\r\n",content.size()+strlen(endsss)+strlen(ends));