2016-03-29 11 views
0

私は以下のコードを持っていますが、nginxは私に500を与えます。私はなぜ助けてください。なぜC++でhttp投稿に失敗しますか?

とヘッダは私が今まで遭遇した任意のerror.someのものを見つけることができない、ここ

ですか?

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

サーバーログを確認しましたが、どうなりますか?あなたは例を使って投稿を試みましたか? CURLまたは他のコマンドラインプログラム? –

+0

@ Joachim Pileborg私は他のツールを使用していますが、正しく動作します。しかし、私のコードで間違いを見つけることはできません。 – heida

答えて

0

は私が間違った場所を見つけ、それがコンテンツの長さであります。 コンテンツ長、ファイルサイズとを含むサイズ以下

------ WebKitFormBoundarybZRmyZBq9p09AotO コンテンツの廃棄:フォームデータ。 name = "file";ファイル名= "/ホーム/ HEIDA /テスト/ 1152_648.png" のContent-Type:アプリケーション/ 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)); 
関連する問題