winhttp在win7下的2个问题,300分送上

ganzhezeng 2011-12-27 04:26:53
大家好,我在使用winhttp编程的时候碰到2个问题,主要是Ntlm代理环境下访问网络方面的问题,百思不得其解,向大家请教:

1.以下这份代码,在xp-Ntlm代理环境下,运行很正常,但是到了win7下,服务器始终返回407.
抓包发现:
在XP:WinhttpSendRequest后,程序开始发包,但是不会自动进行NTLM的验证过程,验证过程需要自己实现(HASH和挑战-响应过程不需要代码实现,程序自动进行),需要自己resend。
具体过程为:
1) Get www.xxx.com
2) Get NtlmSSP_NEGOTIATE
3) Get NtlmSSP_AUTH
然后服务器就返回200.

在win7:WinhttpSendRequest后,程序开始发包,连续进行了3次发包,验证过程已经一次性地发出去了,过程为
1) Get www.xxx.com
2) Get NtlmSSP_NEGOTIATE
3) Get NtlmSSP_AUTH
然后服务器就返回407.再继续resend多少次都是407.win7下浏览器设置代理后可以正常上网。

问题:win7下程序进行NTLM验证需要什么特别的设置吗?

2.winhttp中如果收到302会自动重定向, 有没有办法使得程序不会自动重定向?
(在C#中有这个设置,c++中目前我还没找到方法)

希望大家能指点一二,只能发100分帖,解决问题了,会补上。

...全文
461 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zylayale 2013-09-17
  • 打赏
  • 举报
回复
引用 楼主 ganzhezeng 的回复:
希望大家能指点一二,只能发100分帖,解决问题了,会补上。
大牛求分。。。
zylayale 2013-09-17
  • 打赏
  • 举报
回复
马勒戈壁的,今天再次被此问题困扰…… 原因如下:因为SQUID代理服务器不支持HTTP1.1请求,而WinHttp会默认使用HTTP/1.1,导致失败。 解决方案:1.WinhttpOpenRequest使用HTTP/1.0访问 2.Squid打开对http/1.1的支持 经测试第2步可以解决。坑爹!(但是ntlm+http/1.0支持的代理是否仍会出问题,这点有待测试)
solohac 2011-12-29
  • 打赏
  • 举报
回复
有人能帮看看吗?
ganzhezeng 2011-12-27
  • 打赏
  • 举报
回复


#include "stdafx.h"
#include <windows.h>
#include <winhttp.h>
#include <stdio.h>

#pragma comment(lib, "winhttp.lib")

DWORD ChooseAuthScheme( DWORD dwSupportedSchemes )
{
// It is the server's responsibility only to accept
// authentication schemes that provide a sufficient
// level of security to protect the servers resources.
//
// The client is also obligated only to use an authentication
// scheme that adequately protects its username and password.
//
// Thus, this sample code does not use Basic authentication
// becaus Basic authentication exposes the client's username
// and password to anyone monitoring the connection.

if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NEGOTIATE )
return WINHTTP_AUTH_SCHEME_NEGOTIATE;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NTLM )
return WINHTTP_AUTH_SCHEME_NTLM;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_PASSPORT )
return WINHTTP_AUTH_SCHEME_PASSPORT;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_DIGEST )
return WINHTTP_AUTH_SCHEME_DIGEST;
else
return 0;
}

struct SWinHttpSampleGet
{
LPCWSTR szServer;
LPCWSTR szPath;
BOOL fUseSSL;
LPCWSTR szServerUsername;
LPCWSTR szServerPassword;
LPCWSTR szProxyUsername;
LPCWSTR szProxyPassword;
};

void WinHttpAuthSample( )
{
DWORD dwStatusCode = 0;
DWORD dwSupportedSchemes;
DWORD dwFirstScheme;
DWORD dwSelectedScheme;
DWORD dwTarget;
DWORD dwLastStatus = 0;
DWORD dwSize = sizeof(DWORD);
BOOL bResults = FALSE;
BOOL bDone = FALSE;

DWORD dwProxyAuthScheme = 0;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;

bool testB = false;

DWORD errorCode = 0;

WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig;
WinHttpGetIEProxyConfigForCurrentUser(&proxyConfig);

hSession = WinHttpOpen( L"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1;)",
WINHTTP_ACCESS_TYPE_NAMED_PROXY,
L"192.168.10.109:3128",
WINHTTP_NO_PROXY_BYPASS,
0 );
//
errorCode = GetLastError();

// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession,
L"tieba.baidu.com" ,
INTERNET_DEFAULT_HTTP_PORT,
0 );
//
errorCode = GetLastError();

// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect,
L"GET",
L"/index.html",
L"HTTP/1.1",
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
0 );
//
errorCode = GetLastError();

// Continue to send a request until status code
// is not 401 or 407.
if( hRequest == NULL )
bDone = TRUE;

while( !bDone )
{
// If a proxy authentication challenge was responded to, reset
// those credentials before each SendRequest, because the proxy
// may require re-authentication after responding to a 401 or
// to a redirect. If you don't, you can get into a
// 407-401-407-401- loop.

if( dwProxyAuthScheme != 0 )
bResults = WinHttpSetCredentials( hRequest,
WINHTTP_AUTH_TARGET_PROXY,
// dwProxyAuthScheme,
// WINHTTP_AUTH_SCHEME_NTLM,
dwProxyAuthScheme,
NULL,
NULL,
NULL );
//
errorCode = GetLastError();

if (!testB)
{
const TCHAR contentLogin[] = L"Accept: */*\r\n";
BOOL ret = WinHttpAddRequestHeaders (hRequest, contentLogin, wcslen(contentLogin), WINHTTP_ADDREQ_FLAG_ADD);

//
errorCode = GetLastError();

const TCHAR c1[] = L"Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n";
ret = WinHttpAddRequestHeaders (hRequest, c1, wcslen(c1), WINHTTP_ADDREQ_FLAG_ADD );

const TCHAR c2[] = L"Accept-Encoding: gzip,deflate\r\n";
ret = WinHttpAddRequestHeaders (hRequest, c2, wcslen(c2), WINHTTP_ADDREQ_FLAG_ADD );

const TCHAR c3[] = L"Accept-Language: zh-cn\r\n";
ret = WinHttpAddRequestHeaders (hRequest, c3, wcslen(c3), WINHTTP_ADDREQ_FLAG_ADD );

testB = true;
}




// Send a request.
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
WINHTTP_NO_REQUEST_DATA,
0,
0,
0 );
//
errorCode = GetLastError();

// End the request.
if( bResults )
{
bResults = WinHttpReceiveResponse( hRequest, NULL );
//
errorCode = GetLastError();
}
// Resend the request in case of
// ERROR_WINHTTP_RESEND_REQUEST error.
if( !bResults && GetLastError( ) == ERROR_WINHTTP_RESEND_REQUEST)
{
continue;
//
errorCode = GetLastError();
}

// Check the status code.
if( bResults )
{
bResults = WinHttpQueryHeaders( hRequest,
WINHTTP_QUERY_STATUS_CODE |
WINHTTP_QUERY_FLAG_NUMBER,
NULL,
&dwStatusCode,
&dwSize,
NULL );
//
errorCode = GetLastError();
}

if( bResults )
{
switch( dwStatusCode )
{
case 200:
// The resource was successfully retrieved.
// You can use WinHttpReadData to read the
// contents of the server's response.
printf( "The resource was successfully retrieved.\n" );
bDone = TRUE;
break;

case 401:
// The server requires authentication.
printf(" The server requires authentication. Sending credentials...\n" );

// Obtain the supported and preferred schemes.
bResults = WinHttpQueryAuthSchemes( hRequest,
&dwSupportedSchemes,
&dwFirstScheme,
&dwTarget );

// Set the credentials before resending the request.
if( bResults )
{
dwSelectedScheme = ChooseAuthScheme( dwSupportedSchemes);

if( dwSelectedScheme == 0 )
bDone = TRUE;
else
bResults = WinHttpSetCredentials( hRequest,
WINHTTP_AUTH_TARGET_PROXY,
WINHTTP_AUTH_SCHEME_NTLM,
NULL,
NULL,
NULL );
}

// If the same credentials are requested twice, abort the
// request. For simplicity, this sample does not check
// for a repeated sequence of status codes.
if( dwLastStatus == 401 )
bDone = TRUE;

break;

case 407:
// The proxy requires authentication.
printf( "The proxy requires authentication. Sending credentials...\n" );

// Obtain the supported and preferred schemes.
bResults = WinHttpQueryAuthSchemes( hRequest,
&dwSupportedSchemes,
&dwFirstScheme,
&dwTarget );

// Set the credentials before resending the request.
if( bResults )
dwProxyAuthScheme = ChooseAuthScheme(dwSupportedSchemes);

// If the same credentials are requested twice, abort the
// request. For simplicity, this sample does not check
// for a repeated sequence of status codes.
if( dwLastStatus == 407 )
bDone = TRUE;
break;

default:
// The status code does not indicate success.
printf("Error. Status code %d returned.\n", dwStatusCode);
bDone = TRUE;
}
}

// Keep track of the last status code.
dwLastStatus = dwStatusCode;

// If there are any errors, break out of the loop.
if( !bResults )
bDone = TRUE;
}

// Report any errors.
if( !bResults )
{
DWORD dwLastError = GetLastError( );
printf( "Error %d has occurred.\n", dwLastError );
}

// Close any open handles.
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );
}

int _tmain(int argc, _TCHAR* argv[])
{


WinHttpAuthSample();

return 0;
}


ganzhezeng 2011-12-27
  • 打赏
  • 举报
回复

#include "stdafx.h"
#include <windows.h>
#include <winhttp.h>
#include <stdio.h>

#pragma comment(lib, "winhttp.lib")

DWORD ChooseAuthScheme( DWORD dwSupportedSchemes )
{
// It is the server's responsibility only to accept
// authentication schemes that provide a sufficient
// level of security to protect the servers resources.
//
// The client is also obligated only to use an authentication
// scheme that adequately protects its username and password.
//
// Thus, this sample code does not use Basic authentication
// becaus Basic authentication exposes the client's username
// and password to anyone monitoring the connection.

if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NEGOTIATE )
return WINHTTP_AUTH_SCHEME_NEGOTIATE;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NTLM )
return WINHTTP_AUTH_SCHEME_NTLM;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_PASSPORT )
return WINHTTP_AUTH_SCHEME_PASSPORT;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_DIGEST )
return WINHTTP_AUTH_SCHEME_DIGEST;
else
return 0;
}

struct SWinHttpSampleGet
{
LPCWSTR szServer;
LPCWSTR szPath;
BOOL fUseSSL;
LPCWSTR szServerUsername;
LPCWSTR szServerPassword;
LPCWSTR szProxyUsername;
LPCWSTR szProxyPassword;
};

void WinHttpAuthSample( )
{
DWORD dwStatusCode = 0;
DWORD dwSupportedSchemes;
DWORD dwFirstScheme;
DWORD dwSelectedScheme;
DWORD dwTarget;
DWORD dwLastStatus = 0;
DWORD dwSize = sizeof(DWORD);
BOOL bResults = FALSE;
BOOL bDone = FALSE;

DWORD dwProxyAuthScheme = 0;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;

bool testB = false;

DWORD errorCode = 0;

WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig;
WinHttpGetIEProxyConfigForCurrentUser(&proxyConfig);

hSession = WinHttpOpen( L"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1;)",
WINHTTP_ACCESS_TYPE_NAMED_PROXY,
L"192.168.10.109:3128",
WINHTTP_NO_PROXY_BYPASS,
0 );
//
errorCode = GetLastError();

// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession,
L"tieba.baidu.com" ,
INTERNET_DEFAULT_HTTP_PORT,
0 );
//
errorCode = GetLastError();

// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect,
L"GET",
L"/index.html",
L"HTTP/1.1",
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
0 );
//
errorCode = GetLastError();

// Continue to send a request until status code
// is not 401 or 407.
if( hRequest == NULL )
bDone = TRUE;

while( !bDone )
{
// If a proxy authentication challenge was responded to, reset
// those credentials before each SendRequest, because the proxy
// may require re-authentication after responding to a 401 or
// to a redirect. If you don't, you can get into a
// 407-401-407-401- loop.

if( dwProxyAuthScheme != 0 )
bResults = WinHttpSetCredentials( hRequest,
WINHTTP_AUTH_TARGET_PROXY,
// dwProxyAuthScheme,
// WINHTTP_AUTH_SCHEME_NTLM,
dwProxyAuthScheme,
NULL,
NULL,
NULL );
//
errorCode = GetLastError();

if (!testB)
{
const TCHAR contentLogin[] = L"Accept: */*\r\n";
BOOL ret = WinHttpAddRequestHeaders (hRequest, contentLogin, wcslen(contentLogin), WINHTTP_ADDREQ_FLAG_ADD);

//
errorCode = GetLastError();

const TCHAR c1[] = L"Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n";
ret = WinHttpAddRequestHeaders (hRequest, c1, wcslen(c1), WINHTTP_ADDREQ_FLAG_ADD );

const TCHAR c2[] = L"Accept-Encoding: gzip,deflate\r\n";
ret = WinHttpAddRequestHeaders (hRequest, c2, wcslen(c2), WINHTTP_ADDREQ_FLAG_ADD );

const TCHAR c3[] = L"Accept-Language: zh-cn\r\n";
ret = WinHttpAddRequestHeaders (hRequest, c3, wcslen(c3), WINHTTP_ADDREQ_FLAG_ADD );

testB = true;
}




// Send a request.
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
WINHTTP_NO_REQUEST_DATA,
0,
0,
0 );
//
errorCode = GetLastError();

// End the request.
if( bResults )
{
bResults = WinHttpReceiveResponse( hRequest, NULL );
//
errorCode = GetLastError();
}
// Resend the request in case of
// ERROR_WINHTTP_RESEND_REQUEST error.
if( !bResults && GetLastError( ) == ERROR_WINHTTP_RESEND_REQUEST)
{
continue;
//
errorCode = GetLastError();
}

// Check the status code.
if( bResults )
{
bResults = WinHttpQueryHeaders( hRequest,
WINHTTP_QUERY_STATUS_CODE |
WINHTTP_QUERY_FLAG_NUMBER,
NULL,
&dwStatusCode,
&dwSize,
NULL );
//
errorCode = GetLastError();
}

if( bResults )
{
switch( dwStatusCode )
{
case 200:
// The resource was successfully retrieved.
// You can use WinHttpReadData to read the
// contents of the server's response.
printf( "The resource was successfully retrieved.\n" );
bDone = TRUE;
break;

case 401:
// The server requires authentication.
printf(" The server requires authentication. Sending credentials...\n" );

// Obtain the supported and preferred schemes.
bResults = WinHttpQueryAuthSchemes( hRequest,
&dwSupportedSchemes,
&dwFirstScheme,
&dwTarget );

// Set the credentials before resending the request.
if( bResults )
{
dwSelectedScheme = ChooseAuthScheme( dwSupportedSchemes);

if( dwSelectedScheme == 0 )
bDone = TRUE;
else
bResults = WinHttpSetCredentials( hRequest,
WINHTTP_AUTH_TARGET_PROXY,
WINHTTP_AUTH_SCHEME_NTLM,
NULL,
NULL,
NULL );
}

// If the same credentials are requested twice, abort the
// request. For simplicity, this sample does not check
// for a repeated sequence of status codes.
if( dwLastStatus == 401 )
bDone = TRUE;

break;

case 407:
// The proxy requires authentication.
printf( "The proxy requires authentication. Sending credentials...\n" );

// Obtain the supported and preferred schemes.
bResults = WinHttpQueryAuthSchemes( hRequest,
&dwSupportedSchemes,
&dwFirstScheme,
&dwTarget );

// Set the credentials before resending the request.
if( bResults )
dwProxyAuthScheme = ChooseAuthScheme(dwSupportedSchemes);

// If the same credentials are requested twice, abort the
// request. For simplicity, this sample does not check
// for a repeated sequence of status codes.
if( dwLastStatus == 407 )
bDone = TRUE;
break;

default:
// The status code does not indicate success.
printf("Error. Status code %d returned.\n", dwStatusCode);
bDone = TRUE;
}
}

// Keep track of the last status code.
dwLastStatus = dwStatusCode;

// If there are any errors, break out of the loop.
if( !bResults )
bDone = TRUE;
}

// Report any errors.
if( !bResults )
{
DWORD dwLastError = GetLastError( );
printf( "Error %d has occurred.\n", dwLastError );
}

// Close any open handles.
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );
}

int _tmain(int argc, _TCHAR* argv[])
{


WinHttpAuthSample();

return 0;
}


都忘记上代码了。。
ganzhezeng 2011-12-27
  • 打赏
  • 举报
回复


#include "stdafx.h"
#include <windows.h>
#include <winhttp.h>
#include <stdio.h>

#pragma comment(lib, "winhttp.lib")

DWORD ChooseAuthScheme( DWORD dwSupportedSchemes )
{
// It is the server's responsibility only to accept
// authentication schemes that provide a sufficient
// level of security to protect the servers resources.
//
// The client is also obligated only to use an authentication
// scheme that adequately protects its username and password.
//
// Thus, this sample code does not use Basic authentication
// becaus Basic authentication exposes the client's username
// and password to anyone monitoring the connection.

if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NEGOTIATE )
return WINHTTP_AUTH_SCHEME_NEGOTIATE;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_NTLM )
return WINHTTP_AUTH_SCHEME_NTLM;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_PASSPORT )
return WINHTTP_AUTH_SCHEME_PASSPORT;
else if( dwSupportedSchemes & WINHTTP_AUTH_SCHEME_DIGEST )
return WINHTTP_AUTH_SCHEME_DIGEST;
else
return 0;
}

struct SWinHttpSampleGet
{
LPCWSTR szServer;
LPCWSTR szPath;
BOOL fUseSSL;
LPCWSTR szServerUsername;
LPCWSTR szServerPassword;
LPCWSTR szProxyUsername;
LPCWSTR szProxyPassword;
};

void WinHttpAuthSample( )
{
DWORD dwStatusCode = 0;
DWORD dwSupportedSchemes;
DWORD dwFirstScheme;
DWORD dwSelectedScheme;
DWORD dwTarget;
DWORD dwLastStatus = 0;
DWORD dwSize = sizeof(DWORD);
BOOL bResults = FALSE;
BOOL bDone = FALSE;

DWORD dwProxyAuthScheme = 0;
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;

bool testB = false;

DWORD errorCode = 0;

WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyConfig;
WinHttpGetIEProxyConfigForCurrentUser(&proxyConfig);

hSession = WinHttpOpen( L"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1;)",
WINHTTP_ACCESS_TYPE_NAMED_PROXY,
L"192.168.10.109:3128",
WINHTTP_NO_PROXY_BYPASS,
0 );
//
errorCode = GetLastError();

// Specify an HTTP server.
if( hSession )
hConnect = WinHttpConnect( hSession,
L"tieba.baidu.com" ,
INTERNET_DEFAULT_HTTP_PORT,
0 );
//
errorCode = GetLastError();

// Create an HTTP request handle.
if( hConnect )
hRequest = WinHttpOpenRequest( hConnect,
L"GET",
L"/index.html",
L"HTTP/1.1",
WINHTTP_NO_REFERER,
WINHTTP_DEFAULT_ACCEPT_TYPES,
0 );
//
errorCode = GetLastError();

// Continue to send a request until status code
// is not 401 or 407.
if( hRequest == NULL )
bDone = TRUE;

while( !bDone )
{
// If a proxy authentication challenge was responded to, reset
// those credentials before each SendRequest, because the proxy
// may require re-authentication after responding to a 401 or
// to a redirect. If you don't, you can get into a
// 407-401-407-401- loop.

if( dwProxyAuthScheme != 0 )
bResults = WinHttpSetCredentials( hRequest,
WINHTTP_AUTH_TARGET_PROXY,
// dwProxyAuthScheme,
// WINHTTP_AUTH_SCHEME_NTLM,
dwProxyAuthScheme,
NULL,
NULL,
NULL );
//
errorCode = GetLastError();

if (!testB)
{
const TCHAR contentLogin[] = L"Accept: */*\r\n";
BOOL ret = WinHttpAddRequestHeaders (hRequest, contentLogin, wcslen(contentLogin), WINHTTP_ADDREQ_FLAG_ADD);

//
errorCode = GetLastError();

const TCHAR c1[] = L"Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n";
ret = WinHttpAddRequestHeaders (hRequest, c1, wcslen(c1), WINHTTP_ADDREQ_FLAG_ADD );

const TCHAR c2[] = L"Accept-Encoding: gzip,deflate\r\n";
ret = WinHttpAddRequestHeaders (hRequest, c2, wcslen(c2), WINHTTP_ADDREQ_FLAG_ADD );

const TCHAR c3[] = L"Accept-Language: zh-cn\r\n";
ret = WinHttpAddRequestHeaders (hRequest, c3, wcslen(c3), WINHTTP_ADDREQ_FLAG_ADD );

testB = true;
}




// Send a request.
bResults = WinHttpSendRequest( hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
WINHTTP_NO_REQUEST_DATA,
0,
0,
0 );
//
errorCode = GetLastError();

// End the request.
if( bResults )
{
bResults = WinHttpReceiveResponse( hRequest, NULL );
//
errorCode = GetLastError();
}
// Resend the request in case of
// ERROR_WINHTTP_RESEND_REQUEST error.
if( !bResults && GetLastError( ) == ERROR_WINHTTP_RESEND_REQUEST)
{
continue;
//
errorCode = GetLastError();
}

// Check the status code.
if( bResults )
{
bResults = WinHttpQueryHeaders( hRequest,
WINHTTP_QUERY_STATUS_CODE |
WINHTTP_QUERY_FLAG_NUMBER,
NULL,
&dwStatusCode,
&dwSize,
NULL );
//
errorCode = GetLastError();
}

if( bResults )
{
switch( dwStatusCode )
{
case 200:
// The resource was successfully retrieved.
// You can use WinHttpReadData to read the
// contents of the server's response.
printf( "The resource was successfully retrieved.\n" );
bDone = TRUE;
break;

case 401:
// The server requires authentication.
printf(" The server requires authentication. Sending credentials...\n" );

// Obtain the supported and preferred schemes.
bResults = WinHttpQueryAuthSchemes( hRequest,
&dwSupportedSchemes,
&dwFirstScheme,
&dwTarget );

// Set the credentials before resending the request.
if( bResults )
{
dwSelectedScheme = ChooseAuthScheme( dwSupportedSchemes);

if( dwSelectedScheme == 0 )
bDone = TRUE;
else
bResults = WinHttpSetCredentials( hRequest,
WINHTTP_AUTH_TARGET_PROXY,
WINHTTP_AUTH_SCHEME_NTLM,
NULL,
NULL,
NULL );
}

// If the same credentials are requested twice, abort the
// request. For simplicity, this sample does not check
// for a repeated sequence of status codes.
if( dwLastStatus == 401 )
bDone = TRUE;

break;

case 407:
// The proxy requires authentication.
printf( "The proxy requires authentication. Sending credentials...\n" );

// Obtain the supported and preferred schemes.
bResults = WinHttpQueryAuthSchemes( hRequest,
&dwSupportedSchemes,
&dwFirstScheme,
&dwTarget );

// Set the credentials before resending the request.
if( bResults )
dwProxyAuthScheme = ChooseAuthScheme(dwSupportedSchemes);

// If the same credentials are requested twice, abort the
// request. For simplicity, this sample does not check
// for a repeated sequence of status codes.
if( dwLastStatus == 407 )
bDone = TRUE;
break;

default:
// The status code does not indicate success.
printf("Error. Status code %d returned.\n", dwStatusCode);
bDone = TRUE;
}
}

// Keep track of the last status code.
dwLastStatus = dwStatusCode;

// If there are any errors, break out of the loop.
if( !bResults )
bDone = TRUE;
}

// Report any errors.
if( !bResults )
{
DWORD dwLastError = GetLastError( );
printf( "Error %d has occurred.\n", dwLastError );
}

// Close any open handles.
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );
}

int _tmain(int argc, _TCHAR* argv[])
{


WinHttpAuthSample();

return 0;
}




都忘记上代码了。。
luciferisnotsatan 2011-12-27
  • 打赏
  • 举报
回复
代码在哪里?

64,646

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧