HTTP Post request
-
Hallo wie kann man einen HTTP POST request machen für GET kriege ich es hin:
HTTP Header bei GET Variable [ Den request habe ich mit Live HTTP Header aufgezeichnet ]
http://testaccount435.te.funpic.de/s2.php?login=123 GET /s2.php?login=123 HTTP/1.1 Host: testaccount435.te.funpic.de User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 115 Connection: keep-alive Referer: http://testaccount435.te.funpic.de/s1.php Cookie: cken=1273649640; __utma=170934591.2041639445.1273648971.1273648971.1273648971.1; __utmb=170934591.8.10.1273648971; __utmc=170934591; __utmz=170934591.1273648971.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) HTTP/1.1 200 OK Set-Cookie: cken=1273649659; domain=funpic.de Date: Wed, 12 May 2010 07:34:20 GMT Transfer-Encoding: chunked Connection: Keep-Alive Server: Apache/2.2.11 (Unix) mod_perl/2.0.4 Perl/v5.10.0 Content-Type: text/html; charset=ISO-8859-1 Keep-Alive: timeout=10, max=1000
#include <iostream> #include <windows> using namespace std; int main() { int wsa_ret = 0; int ret_connect = 0; char rec[256]; string req ="GET /s2.php?login=123 HTTP/1.1\r\n"; req+="Host: testaccount435.te.funpic.de\r\n"; req+="\r\n"; SOCKET client = 0; WORD wVersionRequested = MAKEWORD( 2, 2); WSAData lpWSAData; wsa_ret = WSAStartup ( wVersionRequested, &lpWSAData ); if ( wsa_ret != 0) { cout <<"init error"<<endl; } client = socket( AF_INET , SOCK_STREAM , IPPROTO_TCP ); if ( client == INVALID_SOCKET ) { cout <<"invalid socket" <<endl; } sockaddr_in sock_stru; sock_stru.sin_family = AF_INET; sock_stru.sin_addr.s_addr = inet_addr("213.202.225.64"); sock_stru.sin_port = htons(80); ret_connect = connect( client , (sockaddr*)&sock_stru, sizeof(sock_stru) ); if(ret_connect==SOCKET_ERROR) { cout << "connect error" << endl; } else { cout<<"verbunden"<<endl<<endl; } while ( ret_connect != SOCKET_ERROR ) { ret_connect= send(client,req.c_str(),strlen(req.c_str()),0); ret_connect = recv(client, rec, 256, 0); cout<< rec <<endl; system("PAUSE"); } system("PAUSE"); return 0; } //---------------------------------------------------------------------------
Wie geht das ganze jetzt bei POST hat da wer eine Ahnung ?
HTTP Header bei POST Variable [ Den request habe ich mit Live HTTP Header aufgezeichnet ]
http://testaccount435.te.funpic.de/seite2.php POST /seite2.php HTTP/1.1 Host: testaccount435.te.funpic.de User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 (.NET CLR 3.5.30729) Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 115 Connection: keep-alive Referer: http://testaccount435.te.funpic.de/ Cookie: cken=1273649531; __utma=170934591.2041639445.1273648971.1273648971.1273648971.1; __utmb=170934591.5.10.1273648971; __utmc=170934591; __utmz=170934591.1273648971.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) Content-Type: application/x-www-form-urlencoded Content-Length: 9 login=123 HTTP/1.1 200 OK Set-Cookie: cken=1273649537; domain=funpic.de Date: Wed, 12 May 2010 07:32:18 GMT Transfer-Encoding: chunked Connection: Keep-Alive Server: Apache/2.2.11 (Unix) mod_perl/2.0.4 Perl/v5.10.0 Content-Type: text/html; charset=ISO-8859-1 Keep-Alive: timeout=10, max=1000
Die PHP Datei mit der Post Variable:
[Bei der GET Variable habe ich den gleichen PHP Code verwendet nur anstatt POST natürlich die GET Variable benutzt]Index.php
<form action="seite2.php" method="POST" <body> login:<br> <input name = "login"> <br><br> <br> <input type="Submit" value="Absenden"> <br><br> </body> </form>
Seite2.php
<?php $login = $_POST["login"]; if ( $login == "123") { echo "ok"; } else { echo "error"; } ?>
-
POST /pfad/datei.php HTTP/1.1\r\n
Host: deinesendeip\r\n
Referer: wohinreferer\r\n
Content-type: application/x-www-form-urlencoded\r\n
Content-length: strlen(data)\r\n
Connection: close\r\n\r\n
data\r\nbeachte die zeilenumbrüche, nach close und vor den eigentlichen daten müssen zwei umbrüche sein. lies das entsprechende rfc.
cookies etc. habe ich weggelassen.edit: dreher rausgenommen
-
Hallo ich hab es mal versucht aber ich bekomme immer vom Server die Meldung Bad request
Was ist hier dran falsch ?
string req = ("POST 213.202.225.64 HTTP/1.1\r\n"); req+= ("Host: /s2.php?login=123\r\n"); req+=("Referer: http://testaccount435.te.funpic.de/\r\n"); req+=("Content-type: application/x-www-form-urlencoded\r\n"); req+=("Content-length: %d\r\n") , strlen(req.c_str()) ; req+=("Connection: close\r\n\r\n"); req+= (" %d \r\n") , req ;
Mein gesamter Quelltext:
#include <iostream> #include <windows> using namespace std; int main() { int wsa_ret = 0; int ret_connect = 0; char rec[256]; string req = ("POST 213.202.225.64 HTTP/1.1\r\n"); req+= ("Host: /s2.php?login=123\r\n"); req+=("Referer: http://testaccount435.te.funpic.de/\r\n"); req+=("Content-type: application/x-www-form-urlencoded\r\n"); req+=("Content-length: %d\r\n") , strlen(req.c_str()) ; req+=("Connection: close\r\n\r\n"); req+= (" %d \r\n") , req ; SOCKET client = 0; WORD wVersionRequested = MAKEWORD( 2, 2); WSAData lpWSAData; wsa_ret = WSAStartup ( wVersionRequested, &lpWSAData ); if ( wsa_ret != 0) { cout <<"init error"<<endl; } client = socket( AF_INET , SOCK_STREAM , IPPROTO_TCP ); if ( client == INVALID_SOCKET ) { cout <<"invalid socket" <<endl; } sockaddr_in sock_stru; sock_stru.sin_family = AF_INET; sock_stru.sin_addr.s_addr = inet_addr("213.202.225.64"); sock_stru.sin_port = htons(80); ret_connect = connect( client , (sockaddr*)&sock_stru, sizeof(sock_stru) ); if(ret_connect==SOCKET_ERROR) { cout << "connect error" << endl; } else { cout<<"verbunden"<<endl<<endl; } while ( ret_connect != SOCKET_ERROR ) { ret_connect= send(client,req.c_str(),strlen(req.c_str()),0); ret_connect = recv(client, rec, 256, 0); cout<< rec <<endl; system("PAUSE"); } system("PAUSE"); return 0; } //---------------------------------------------------------------------------
-
die daten müssen als post hinten angehängt werden nach zwei zeilenumbrüchen, NACH den close. sonst schickst du sie per get
string data = "login=123"; //dann über deine Zeichenketten nachdenken. hier nur die logik: POST /s2.php HTTP/1.1\r\n Host:213.202.225.64 \r\n Referer: http://testaccount435.te.funpic.de/\r\n Content-type: application/x-www-form-urlencoded\r\n Content-length: 9 \r\n //über strlen(data.c_str()) Connection: close\r\n\r\n //hier dann nach den zwei umbrüchen die daten hinterher data \r\n
-
Was ist da jetzt wieder falsch wieso geht das nicht ?
BAD REQUEST ich kanns bald nicht mehr sehen. >_<
string data = "login=123"; string req = "POST seite2.php HTTP/1.1\r\n"; req+= "Host: 213.202.225.64 \r\n"; req+= "Referer: http://testaccount435.te.funpic.de/\r\n"; req+="Content-type: application/x-www-form-urlencoded\r\n"; req+=("Content-length: %d \r\n"), strlen(data.c_str() ); req+= "Connection: close\r\n\r\n"; req+= data , "\r\n";
-
Hinter "POST" muss entweder eine relative URL, beginnend mit "/" folgen ("POST /seite2.php HTTP/1.1") oder die absolute URL ("POST http://www.example.org/seite2.php HTTP/1.1")
-
Bad request
string data = "login=123"; string req = "POST /seite2.php HTTP/1.1\r\n"; req+= "Host: 213.202.225.64 \r\n"; req+= "Referer: http://testaccount435.te.funpic.de/\r\n"; req+="Content-type: application/x-www-form-urlencoded\r\n"; req+=("Content-length: %d \r\n"), strlen(data.c_str() ); req+= "Connection: close\r\n\r\n"; req+= data , "\r\n"
Auch bad request
string data = "login=123"; string req = "POST http://testaccount435.te.funpic.de/seite2.php HTTP/1.1\r\n"; req+= "Host: 213.202.225.64 \r\n"; req+= "Referer: http://testaccount435.te.funpic.de/\r\n"; req+="Content-type: application/x-www-form-urlencoded\r\n"; req+=("Content-length: %d \r\n"), strlen(data.c_str() ); req+= "Connection: close\r\n\r\n"; req+= data , "\r\n";
Wieso geht das nicht ?
QQ
-
POST /seite2.php HTTP/1.1 Host: testaccount435.te.funpic.de Referer: http://testaccount435.te.funpic.de/ Content-Type: application/x-www-form-urlencoded Content-Length: 9 Connection: close login=123
...das meldet bei mir nen "200 OK"
-
Wieso denn bei mir nicht >_<
Ich verstehs nicht -.-
Hier ist mein Kompletter Code:
#include <iostream> #include <windows> using namespace std; int main() { int wsa_ret = 0; int ret_connect = 0; char rec[256]; string data = "login=123"; string req = "POST http://testaccount435.te.funpic.de/seite2.php HTTP/1.1\r\n"; req+= "Host: 213.202.225.64 \r\n"; req+= "Referer: http://testaccount435.te.funpic.de/\r\n"; req+="Content-type: application/x-www-form-urlencoded\r\n"; req+=("Content-length: %d \r\n"), strlen(data.c_str() ); req+= "Connection: close\r\n\r\n"; req+= data , "\r\n"; SOCKET client = 0; WORD wVersionRequested = MAKEWORD( 2, 2); WSAData lpWSAData; wsa_ret = WSAStartup ( wVersionRequested, &lpWSAData ); if ( wsa_ret != 0) { cout <<"init error"<<endl; } client = socket( AF_INET , SOCK_STREAM , IPPROTO_TCP ); if ( client == INVALID_SOCKET ) { cout <<"invalid socket" <<endl; } sockaddr_in sock_stru; sock_stru.sin_family = AF_INET; sock_stru.sin_addr.s_addr = inet_addr("213.202.225.64"); sock_stru.sin_port = htons(80); ret_connect = connect( client , (sockaddr*)&sock_stru, sizeof(sock_stru) ); if(ret_connect==SOCKET_ERROR) { cout << "connect error" << endl; } else { cout<<"verbunden"<<endl<<endl; } while ( ret_connect != SOCKET_ERROR ) { ret_connect= send(client,req.c_str(),strlen(req.c_str()),0); ret_connect = recv(client, rec, 256, 0); cout<< rec <<endl; system("PAUSE"); } system("PAUSE"); return 0; }
-
oops
seite1.php muss eigentlich verwendet werden bei mir weil da hab ich die php Datei die einen Post request entgegenehmen kann:
Also:
string req = "POST http://testaccount435.te.funpic.de/seite1.php HTTP/1.1\r\n";
anstatt:
string req = "POST http://testaccount435.te.funpic.de/seite2.php HTTP/1.1\r\n";
Aber es funktioniert trotzdem nicht bei mir.
-
ein einfaches
cout<< req;hätte dir gezeigt: wie kommst du darauf, dass %d ersetzt wird? prüfe deine zeichenkette!
-
Ja cout<<req; jetzt wird mir alles klar xD
So funktioniert es nun endlich bei mir.
string data = "login=123"; string req = "POST http://testaccount435.te.funpic.de/seite2.php HTTP/1.1\r\n"; req+= "Host: 213.202.225.64 \r\n"; req+= "Referer: http://testaccount435.te.funpic.de/\r\n"; req+="Content-type: application/x-www-form-urlencoded\r\n"; req+="Content-length: 9\r\n"; req+= "Connection: close\r\n\r\n"; req+= data , "\r\n";
Nur wie ich das ganze mit strlen lösen kann das krieg ich leider nicht hin :(.
Hab ich noch nie gemacht :(.
-
Eine Frage noch wer kennt eine gute Seite wo alle diese Sachen nochmals erklärt werden ?
Bisher hab ich bei google nichts wirklich brauchbares gefunden.