c++ - Winsock program getting 400 bad request, your browser sent a request this server couldn't understand -
i made program in c++ using winsock2 , connected godaddy.
http/1.1 400 bad request date: mon, 17 aug 2015 01:53:38 gmt server: apache content-length: 300 connection: close content-type: text/html; charset=iso-8859-1 <!doctype html public "-//ietf//dtd html 2.0//en"> <html><head> <title>400 bad request</title> </head><body> <h1>bad request</h1> <p>your browser sent request server not understand.<br /> </p> <hr> <address>apache server @ default.secureserver.net port 80</address> </body></html>
here's code:
#include <windows.h> #include <winsock2.h> #include <conio.h> #include <stdio.h> #include <iostream> using namespace std; #define sck_version2 0x0202 #define default_buflen 2000 #define default_port 27015 namespace globals{ extern string input = ""; } using namespace globals; int whole() { //username(); //password(); //---------------------- // declare , initialize variables. wsadata wsadata; int iresult; socket connectsocket = invalid_socket; struct sockaddr_in clientservice; char name[500] = ""; char ipaddress[500] = ""; char sport[500] = ""; sockaddr_in sname; int snamesize = sizeof(sname); char *sendbuf = "get /offers/online-business.aspx http/1.1 \nhost: took out, not giving myself away \nuser-agent: mozilla/5.0 (windows; u; windows nt 6.1; en-us; rv:1.9.1.5) gecko/20091102 firefox/3.5.5 (.net clr 3.5.30729) \naccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 \naccept-language: en-us,en;q=0.5 \naccept-encoding: gzip,deflate \naccept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.7 \nkeep-alive: 300 \nconnection: keep-alive \ncookie: \npragma: no-cache \ncache-control: no-cache"; char recvbuf[default_buflen]; int recvbuflen = default_buflen; //208.109.181.178 int wsaerror = wsagetlasterror(); //system("color 04"); //---------------------- // initialize winsock iresult = wsastartup(makeword(2,2), &wsadata); if (iresult != no_error) { printf("wsastartup failed: %d\n", iresult); return 1; } //---------------------- // create socket connecting server connectsocket = socket(af_inet, sock_stream, ipproto_tcp); if (connectsocket == invalid_socket) { printf("error @ socket(): %i\n", wsagetlasterror() ); wsacleanup(); return 1; } //---------------------- // sockaddr_in structure specifies address family, // ip address, , port of server connected to. printf("ip address: \n"); cin >> ipaddress; printf("port: \n"); cin >> sport; u_short port = strtoul(sport, null, 0); clientservice.sin_family = af_inet; clientservice.sin_addr.s_addr = inet_addr(ipaddress); //74.125.196.191 clientservice.sin_port = htons(port); //---------------------- // connect server. iresult = connect( connectsocket, (sockaddr*) &clientservice, sizeof(clientservice) ); if ( iresult == socket_error) { closesocket (connectsocket); printf("unable connect server: %i\n", wsagetlasterror()); wsacleanup(); return 1; } //---------------------- //get local host name iresult = gethostname(name, sizeof(name)); if (iresult == no_error) { printf("host name: %s\n", name); } else if (iresult == socket_error) { printf("could not resolve host name: %i", wsagetlasterror()); } //------------------------ //get peer name iresult = getpeername(connectsocket, (struct sockaddr*)&sname, &snamesize); if (iresult == no_error) printf("peer name: %s\n", inet_ntoa(sname.sin_addr)); else if (iresult == socket_error) printf("could not peer name: %i\n", wsagetlasterror()); //------------------------- // send initial buffer iresult = send( connectsocket, sendbuf, (int)strlen(sendbuf), 0 ); if (iresult == socket_error) { printf("send failed: %d\n", wsagetlasterror()); closesocket(connectsocket); wsacleanup(); return 1; } else printf("bytes sent: %i\n", iresult); //----------------------------- // shutdown connection since no more data sent iresult = shutdown(connectsocket, sd_send); if (iresult == socket_error) { printf("shutdown failed: %d\n", wsagetlasterror()); closesocket(connectsocket); wsacleanup(); return 1; } // receive until peer closes connection { iresult = recv(connectsocket, recvbuf, recvbuflen, 0); if ( iresult > 0 ) { printf("bytes received: %d\n", iresult); //printf("bytes received: %d\n", iresult); printf("from server: %s\n", recvbuf); } else if ( iresult == 0 ) printf("connection closed\n"); else if (wsaerror == wsaetimedout) printf("recv failed: wsaetimedout\n"); printf("do want disconnect? (y/n) \n"); cin >> input; if ( input == "y"||"y" ) { break; } else if ( input == "n"||"n" ) { break; } } while( iresult > 0 ); // cleanup closesocket(connectsocket); wsacleanup(); system("pause"); return 0; } int main() { { whole(); } while( input != "n"||"n" ); }
sendbuf
contains request. example found online showed looking specific directory. more ask directory , specific html file or not? if tell me parameters or how make request appreciated.
edit: know if might have \r\n
?
one thing @ http headers, appear incorrectly formatted.
each line must terminated \r\n, , entire header block \r\n\r\n.
also, cookie
header has no value - not sure if causing issue.
try (added \r after each header, \r\n\r\n terminate block, removed cookie
:
char *sendbuf = "get /offers/online-business.aspx http/1.1\r\n" "host: took out, not giving myself away\r\n" "user-agent: mozilla/5.0 (windows; u; windows nt 6.1; en-us; rv:1.9.1.5) gecko/20091102 firefox/3.5.5 (.net clr 3.5.30729)\r\n" "accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" "accept-language: en-us,en;q=0.5\r\n" "accept-encoding: gzip,deflate\r\n" "accept-charset: iso-8859-1,utf-8;q=0.7,*;q=0.7\r\n" "keep-alive: 300\r\n" "connection: keep-alive\r\n" "pragma: no-cache\r\n" "cache-control: no-cache\r\n\r\n";
Comments
Post a Comment