如何使用POCO使用HTTP基本身份验证创建HTTP Post?

Aly*_*hak 3 passwords post http basic-authentication poco-libraries

我正在尝试使用POCO制作带有HTTP基本身份验证的HTTP帖子(明文用户名和密码).我找到了一个Get的例子,并尝试修改它,但作为一个新手,我想我已经把它毁了它的用处.有人知道怎么做吗?

是的,我已经看到了另一个问题:POCO C++ - NET SSL - 如何发布HTTPS请求,但我无法理解它是如何尝试实现用户名和密码部分的.我也不明白"x-www-form-urlencoded"的用法.邮政需要这个吗?我没有表格.只想用用户名和密码参数POST到服务器.

Aly*_*hak 10

最后.这是你如何做到的:

    HTTPClientSession session("yourdomain.com");
    session.setKeepAlive(true);

    Poco::Net::HTTPRequest req(Poco::Net::HTTPRequest::HTTP_POST, "/myapi.php/api/validate", HTTPMessage::HTTP_1_1);
    req.setContentType("application/x-www-form-urlencoded");
    req.setKeepAlive(true); // notice setKeepAlive is also called on session (above)

    std::string reqBody("username=user1@yourdomain.com&password=mypword");
    req.setContentLength( reqBody.length() );

    std::ostream& myOStream = session.sendRequest(req); // sends request, returns open stream
    myOStream << reqBody;  // sends the body

    req.write(std::cout);

    Poco::Net::HTTPResponse res;
    std::istream& iStr = session.receiveResponse(res);  // get the response from server
    std::cerr << iStr.rdbuf();  // dump server response so you can view it
Run Code Online (Sandbox Code Playgroud)