如何使用POCO使用HTTP基本身份验证创建HTTP Post? [英] How do I make an HTTP Post with HTTP Basic Authentication, using POCO?
问题描述
我正在尝试使用POCO制作带有HTTP基本身份验证的HTTP帖子(明文用户名和密码)。我找到了一个Get的例子,并尝试修改它,但作为一个新手,我想我已经把它破坏了它的实用性。有人知道怎么做吗?
I'm trying to make an HTTP Post with HTTP Basic Authentication (cleartext username and password), using POCO. I found an example of a Get and have tried to modify it, but being a rookie I think I've mangled it beyond usefulness. Anyone know how to do this?
是的,我已经看到了另一个问题: POCO C ++ - NET SSL - 如何发布HTTPS请求,但我无法理解它是如何尝试的实现用户名和密码部分。我也不明白使用x-www-form-urlencoded。邮政需要这个吗?我没有表格。只想用用户名和密码参数POST到服务器。
Yes, I've already seen the other SO question on this: POCO C++ - NET SSL - how to POST HTTPS request, but I can't make sense of how it is trying to implement the username and password part. I also don't understand the use of "x-www-form-urlencoded". Is this required for a Post? I don't have a form. Just want to POST to the server with username and password parameters.
推荐答案
最后。以下是您的操作方法:
Finally. Here's how you do it:
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
这篇关于如何使用POCO使用HTTP基本身份验证创建HTTP Post?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!