我正在将 POCO 用于某些服务客户端。客户端应使用摘要身份验证登录。
POCO documentation claims that digest authentication is supported .
This is a utility class for working with HTTP authentication (basic or digest) in HTTPRequest objects.
这是测试(gtest)的完整源代码,显示存在一些问题:
#include "UnitTest.h"
#include <Poco/Net/HTTPRequest.h>
#include <Poco/Net/HTTPResponse.h>
#include <Poco/Net/HTTPSClientSession.h>
#include <Poco/Net/HTTPClientSession.h>
#include <Poco/StreamCopier.h>
#include <Poco/Net/HTTPCredentials.h>
using namespace Poco::Net;
TEST(PocoDigestAuthTest, HttpBibOrgTest) {
HTTPClientSession session;
session.setHost("httpbin.org");
HTTPRequest request(
"GET",
// "http://httpbin.org/basic-auth/user/passwd", // basic
"http://httpbin.org/digest-auth/auth/user/passwd", // digest
HTTPMessage::HTTP_1_1);
session.sendRequest(request);
HTTPResponse response;
std::istream& firstResponseStream = session.receiveResponse(response);
std::stringstream firstStrStream;
Poco::StreamCopier::copyStream(firstResponseStream, firstStrStream);
ASSERT_EQ(HTTPResponse::HTTP_UNAUTHORIZED, response.getStatus());
HTTPCredentials creds("user", "passwd");
creds.authenticate(request, response);
session.sendRequest(request);
std::istream& bodyStream = session.receiveResponse(response);
EXPECT_NE(HTTPResponse::HTTP_UNAUTHORIZED, response.getStatus());
EXPECT_EQ(HTTPResponse::HTTP_OK, response.getStatus());
std::stringstream strStream;
Poco::StreamCopier::copyStream(bodyStream, strStream);
EXPECT_NE("", strStream.str());
}
此测试在预期状态 HTTP_OK 的情况下失败,我收到 401 (HTTP_UNAUTHORIZED),因此之前的检查也失败。
如果我将 uri 更改为具有基本身份验证的站点,一切都会按预期工作(测试通过)。
我做错了什么?还是 POCO 的一个错误?我能以某种方式修复它吗?
最佳答案
经过一些测试和思考后发现,该问题与摘要身份验证无关。您还需要发送 httpbin.org 服务器设置的 cookie (fake=fake_value)。 Here是您的示例的修改版本,可以使用。请注意对 setCookies() 的额外调用。另外,创建 HTTPRequest 时,只需提供路径,而不是整个 URI。
关于c++ - 使用 POCO 进行客户端摘要身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29118669/