4

我目前正在libcurl(version 7.19.6 built with SPNEGO and GSS-Negotiate support)编写一个客户端(C++/C),它连接到KerberosTomcat 服务器后面的受保护网页(受保护)。使用命令行:-

curl --negotiate -u: http://prtotectedpage.jsp --verbose

这有效(服务器返回一个未经授权的 HTTP 401,然后它允许SPNEGO传递和处理令牌,我可以访问受保护的页面)。

但是,当我编写以下代码并尝试时:-

using namespace std;
#include <stdio.h>
#include <curl.h>
#define YOUR_URL "http://protectedpage.jsp"
#define ANYUSER ""

int main(int argc, char* argv[])
{
    __asm int 3;               //a debugging thing

//initialize a curl object
CURLcode result;
int x;
CURL* curl = curl_easy_init();                   
if(curl){
    curl_easy_setopt(curl,CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);         
    curl_easy_setopt(curl,CURLOPT_USERNAME, ANYUSER);                       
    curl_easy_setopt(curl,CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL,YOUR_URL);
    curl_easy_perform(curl);
    curl_easy_cleanup(curl);                            
}
return 0;
}

在与页面相对应的初始连接(错误 302)被临时移动后,我得到了服务器的响应。

有谁知道这怎么可能发生。

其他一些信息配置(KDC = Windows Active Directory in Windows server 2008),

curl version(7.19.6)

IDE = (Microsoft visual studio)

好的,我对wireshark进行了更多调查,发现他们的初始请求之间存在以下差异:-

对于命令行一(即成功的):-

GET /protected.jsp HTTP 1.1 \r\n
Host : somecomputername
User Agent: curl(7.19.6) (ipc-386-win32) libcurl/7.19.16 OPENSSL/0.9.8K \r\n
Accept */*\r\n
Full request: [http://somecomputername/protected.jsp]

而对于客户端代码(我编写但失败的代码):-

GET /protected.jsp HTTP 1.1 \r\n
Host : somecomputername   
Accept */*\r\n
Full request: [http://somecomputername/protected.jsp]

这意味着用户代理没有在程序中传递。我仍在研究它,一些输入将不胜感激

第二次编辑:-我对两者的详细输出进行了观察:-

对于命令行版本(工作一个) -

> GET /examples/ HTTP/1.1
> User-Agent: curl/7.19.6 (i386-pc-win32) libcurl/7.19.6 OpenSSL/0.9.8k
> Host: somecomputer
> Accept: */*

而对于非工作的(我写的客户端代码): -

> GET /examples HTTP/1.1
Authorization: Basic RGt1bUByMTIzOg==
User-Agent: curl/7.19.6 (i386-pc-win32) libcurl/7.19.6 OpenSSL/0.9.8k
Host: somecomputer
Accept: */*

这两行都是各自 .exe 文件输出的前几行。现在我注意到两件事。默认情况下,失败的一个会转到基本。两个(这个更令人不安),在失败的一个中用户代理和主机行中没有箭头(>)。这是否意味着永远不会发送用户代理?

4

1 回答 1

3

好的,经过 nm 的一些指示和一些在这里和那里的挣扎,我终于让它工作了,我终于得到了一个工作代码:-

using namespace std;

#include "stdafx.h"
#include <stdio.h>
#include <curl.h>

#define YOUR_URL "http://invr28ppqa24:8080/examples"
#define ANYUSER ""
#define ANYPWD ""

int main(int argc, char* argv[])
{
//__asm int 3;

//initialize a curl object
CURLcode result;
int x;
CURL* curl = curl_easy_init();                  //initialize a easy curl handle
if(curl){
    curl_easy_setopt(curl,CURLOPT_USERNAME, ANYUSER);                       //set second option to enable anyuser, a trick necessary for program to work
    curl_easy_setopt(curl,CURLOPT_USERNAME, ANYPWD);
    curl_easy_setopt(curl,CURLOPT_VERBOSE, 1);
    curl_easy_setopt(curl, CURLOPT_URL,YOUR_URL);
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, "false");
    curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.19.6 (i386-pc-win32) libcurl/7.19.6 OpenSSL/0.9.8k");     
    curl_easy_setopt(curl,CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE);         //set first option to enable gssnegotiate authentication
    curl_easy_perform(curl);
    //curl_easy_cleanup(curl);
    scanf("%d", &x);                            //last statement used to get delay in demo situations
}
return 0;

}

302 仍然出现,我必须手动设置用户代理(不是那么优雅)。由于跟随位置,问题基本得到解决。谢谢。

最初的 302 错误仍然存​​在并且没有得到答复。

于 2013-03-19T09:56:34.263 回答