使用 Java 脚本和 C++ 使用用户名和密码登录的 POST HTTP 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5216006/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:41:42  来源:igfitidea点击:

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

c++httppostlogin

提问by Mohamad Elmasri

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

我一直在开发一个应用程序,C++有些部分需要连接到 http 服务器,我设法发送GETHEAD请求并检索网页。

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

目前,我正在尝试向需要登录信息的网站发送POST请求表单,它类似于GET请求,但是,我不知道如何编写它。

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 POSTrequest!?

我该如何写我的POST请求!?

The website uses a form authentication. I mean there is a box to enter the email address and a password and then hit submit. to my knowledge it is written in JS.

该网站使用表单身份验证。我的意思是有一个框可以输入电子邮件地址和密码,然后点击提交。据我所知,它是用JS编写的。

Any help or direction is appreciated.

任何帮助或方向表示赞赏。

回答by Boofhead

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.

我认为“25”是正确的长度。

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.

作为练习,您可以下载允许调试的 HTTP 工具,例如 WireShark,然后登录到没有安全登录系统的通用论坛,例如通常用于 phpbb 论坛的系统。

回答by ferosekhanj

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).

POST 是 GET、HEAD 等 HTTP 方法之一,关于 HTTP 本身的基础知识很少。它是一个基于文本的协议(您可以使用像 putty 这样的 telnet 客户端连接到服务器的端口 80,然后手动键入整个请求)。

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.

第一行包含方法(GET、HEAD、POST 等)、URL 路径和您支持的 HTTP 协议版本。我建议使用 HTTP1.0,因为您不会在同一个连接中发送多个请求。这更难实施。

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

然后发送的是请求的正文。信息必须经过 urlencoded。通读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.

现在来解决您的问题,如果您已经知道如何执行 GET,那么假设 POST 需要以下步骤。

  1. The request will be "POST HTTP/1.0"
  2. Send atleast these headers
  1. 请求将是“POST HTTP/1.0”
  2. 至少发送这些标题

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

内容类型:应用程序/x-www-form-urlencoded 内容长度:

  1. As body send the urlencoded text containing the inputname1=value1&inputname2=valule2
  2. Now you should get the information back from the server.
  1. 作为正文发送包含 inputname1=value1&inputname2=value2 的 urlencoded 文本
  2. 现在您应该从服务器获取信息。

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

希望这可以帮助。如果您想了解更多信息,请阅读http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html