开发者

POST HTTP request to login with username and password with Java script and c++

I have been developing an application with C++ and some parts require connection to http servers, I managed to send GET and HEAD requests and retrieve the webpage.

Currently, I'm trying to send a POST request form to a website that requires login in informations, It is similar to the GET request, however, I don't know how to write it.

let's say:

POST /users/login HTTP/1.1
HOST: www.example.com
Content-Length: 50, 
username: ME
password: pass

and then receive authentication to retrieve login required pages,

How can I write my POST request!?

The website uses a form authentication. I mean there is a box to ente开发者_C百科r the email address and a password and then hit submit. to my knowledge it is written in JS.

Any help or direction is appreciated.


Try rewriting your request as:

POST /users/login HTTP/1.1
HOST: www.example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 25

username=ME&password=pass

I think '25' is the correct length.

As an exercise you could download HTTP tools that allow debugging like WireShark and then log in to a general forum that doesn't have secure log in systems like those typically used with phpbb forums.


The POST is one among the http methods like GET,HEAD etc., Few basics regarding the the HTTP itself. It is a text based protocol (You can connect to port 80 of the server using a telnet client like putty and type the whole request by hand).

The first line contains the method (GET,HEAD,POST etc.,) the URL path and the HTTP protocol version you support. I recommend to use HTTP1.0 since you wont send multiple request within the same connection. This is harder to implement.

The next set of lines are called headers each line contains a key(content-type,content-length, host etc.,) and a value separated by a ':'.

Then whatever is sent is the body of the request. The information has to be urlencoded. Read through http://www.w3schools.com/tags/ref_urlencode.asp

Now coming to your problem if you already know how to perform GET then with that assumption for a POST following steps are required.

  1. The request will be "POST HTTP/1.0"
  2. Send atleast these headers

Content-Type: application/x-www-form-urlencoded Content-Length:

  1. As body send the urlencoded text containing the inputname1=value1&inputname2=valule2
  2. Now you should get the information back from the server.

Hope this helps. If you want to know more read through the http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜