c++ - 使用 POCO 进行客户端摘要身份验证

标签 c++ http poco-libraries digest-authentication

我正在将 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/

相关文章:

c++ - MSVC++ 2008/2010 调试配置中的 std::priority_queue 错误/问题

C++ - 在需要 Iterator 类型的 vector (或其他容器)构造函数中使用数组/指针。这怎么可能?

C++ 使用 ShowWindow 恢复窗口会禁用其最小化功能

c++ - 未收到 Poco::Net::HTTPClientSession json 数据内容类型

c++ - 我可以使用 Poco 从内存缓冲区创建多文件 zip 存档吗?

c++ - 如何读取 C++ 崩溃信息

java - 使用 HttpUrlConnection 在没有任何参数的情况下在服务器上执行 php 脚本

json - 是否值得从 Web 应用程序的 JSON 服务器响应中排除空字段以减少流量?

javascript - 如何删除 xhr 请求中的默认 header ?

c++ - Poco::DOMParser 和 wstring