012
27.12.2015, 05:48 Uhr
Atreju
|
So, ich hab mir nochmal ein Beispielprogramm rausgesucht:
C++: |
/* * http_post.cpp * * by Uday Chitragar - 2004/Dec/01 * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any * damages arising from the use of this software. * * Permission is granted to anyone to use this software for any * purpose, including commercial applications, and to alter it and * redistribute it freely, subject to the following restrictions: * * 1. The origin of this software must not be misrepresented; you must * not claim that you wrote the original software. If you use this * software in a product, an acknowledgment in the product documentation * would be appreciated but is not required. * * 2. Altered source versions must be plainly marked as such, and * must not be misrepresented as being the original software. * * 3. This notice may not be removed or altered from any source * distribution. * * */
/* * Notes: * This source demonstrates sending HTTP POST request to webserver from C++ * This uses sockets hence can be compiled on Linux, UNIX, Win */
#include "stdafx.h" #include <iostream> #include <string> #include <stdlib.h> #include <assert.h> #include <Winsock2.h>
#define SEND_RQ(MSG) \ /*cout<<send_str;*/ \ send(sock,MSG,strlen(MSG),0);
using namespace std; //<exe> hostname api parameters int request(char* hostname, char* api, char* parameters, string& message) { WSADATA WsaData; WSAStartup(0x0101, &WsaData);
sockaddr_in sin; int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) {return -100;} sin.sin_family = AF_INET; sin.sin_port = htons((unsigned short)80);
struct hostent * host_addr = gethostbyname(hostname); if (host_addr == NULL) {return -103;} sin.sin_addr.s_addr = *((int*)*host_addr->h_addr_list);
if (connect(sock, (const struct sockaddr *)&sin, sizeof(sockaddr_in)) == -1) {return -101;}
string send_str;
SEND_RQ("POST "); SEND_RQ(api); SEND_RQ(" HTTP/1.0\r\n"); SEND_RQ("Accept: */*\r\n"); SEND_RQ("User-Agent: Mozilla/4.0\r\n");
char content_header[100]; sprintf(content_header, "Content-Length: %d\r\n", strlen(parameters)); SEND_RQ(content_header); SEND_RQ("Accept-Language: en-us\r\n"); SEND_RQ("Accept-Encoding: gzip, deflate\r\n"); SEND_RQ("Host: "); SEND_RQ("hostname"); SEND_RQ("\r\n"); SEND_RQ("Content-Type: application/x-www-form-urlencoded\r\n");
//If you need to send a basic authorization //string Auth = "username:password"; //Figureout a way to encode test into base64 ! //string AuthInfo = base64_encode(reinterpret_cast<const unsigned char*>(Auth.c_str()),Auth.length()); //string sPassReq = "Authorization: Basic " + AuthInfo; //SEND_RQ(sPassReq.c_str());
SEND_RQ("\r\n"); SEND_RQ("\r\n"); SEND_RQ(parameters); SEND_RQ("\r\n"); char c1[1]; int l, line_length; bool loop = true; bool bHeader = false;
while (loop) { l = recv(sock, c1, 1, 0); if (l<0) loop = false; if (c1[0] == '\n') { if (line_length == 0) loop = false;
line_length = 0; if (message.find("200") != string::npos) bHeader = true;
} else if (c1[0] != '\r') line_length++; message += c1[0]; }
message = ""; if (bHeader) { char p[1024]; while ((l = recv(sock, p, 1023, 0)) > 0) { p[l] = '\0'; message += p; } } else {return -102;} WSACleanup(); return 0; }
int main(){ string message; int request("www.somesite.com", "/post_url.pl", "search=hello&date=todat", string& message); // message contains response!
}
|
Zeile 127 ist die int request(); in der main. Der Compiler meint dazu:
Zitat: |
c:\users\user\documents\visual studio 2013\projects\dltest3\dltest3\dltest3.cpp(72): warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\stdio.h(356): Siehe Deklaration von 'sprintf' 1>c:\users\user\documents\visual studio 2013\projects\dltest3\dltest3\dltest3.cpp(127): error C2275: 'std::string': Ungültige Verwendung dieses Typs als Ausdruck 1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\xstring(2643): Siehe Deklaration von 'std::string' 1>c:\users\user\documents\visual studio 2013\projects\dltest3\dltest3\dltest3.cpp(127): error C2078: Zu viele Initialisierungen 1>c:\users\user\documents\visual studio 2013\projects\dltest3\dltest3\dltest3.cpp(127): error C2440: 'Initialisierung': 'const char [24]' kann nicht in 'int' konvertiert werden 1> Es gibt keinen Kontext, in dem diese Konvertierung möglich ist ========== Erstellen: 0 erfolgreich, 1 fehlerhaft, 0 aktuell, 0 übersprungen ==========
|
Den Code habe ich auf den Windows-Teil beschnitten und eine Debug-Ausgabe entfernt, sonst schmeißt das Ding mit noch mehr Fehlern um sich. Es ist echt zum Kotzen, macht keinen Spaß. PHP ist da wesentlich einfacher. Dieser Post wurde am 27.12.2015 um 05:52 Uhr von Atreju editiert. |