In case of proxy server Winhttp Authentication fails





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2















#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#pragma comment(lib, "winhttp.lib")
int main()
{
static BOOL bRet = FALSE;
TCHAR szHostName[MAX_PATH] = L"www.google.com";
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
const TCHAR BROWSER_INFO = L"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
const WCHAR *WWIZHTTP_TYPES = { L"Accept: image/gif", L"image/x-xbitmap", L"image/jpeg", L"image/pjpeg", L"application/x-shockwave-flash", L"application/x-ms-application", L"application/x-ms-xbap", L"application/vnd.ms-xpsdocument", L"application/xaml+xml", L"application/msword", L"application/vnd.ms-excel", L"application/x-cabinet-win32-x86", L"application/x-pe-win32-x86", L"application/octet-stream", L"application/x-setupscript", L"*//*", NULL };

hSession = WinHttpOpen(BROWSER_INFO, WINHTTP_ACCESS_TYPE_NAMED_PROXY, L"172.168.1.196:808", L"<local>", 0);

if (hSession)
{
hConnect = WinHttpConnect(hSession, szHostName, INTERNET_DEFAULT_HTTP_PORT, 0);
}
if (hConnect)
{
hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL, NULL, WINHTTP_NO_REFERER, WWIZHTTP_TYPES, 0);
}
if (hRequest)
{

//provide username and password

WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_PROXY, WINHTTP_AUTH_SCHEME_BASIC, L"abc", L"abc", NULL);

// Send a request.
bRet = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);

// End the request.
if (bRet)
bRet = WinHttpReceiveResponse(hRequest, NULL);

if (bRet)
{
DWORD dwStatusCode = 0;
DWORD dwTemp = sizeof(dwStatusCode);
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatusCode, &dwTemp, NULL);
if ((dwStatusCode >= HTTP_STATUS_CONTINUE) && (dwStatusCode < HTTP_STATUS_BAD_REQUEST))
{
bRet = TRUE;
}
else
{
bRet = FALSE;
}
}
if (bRet == TRUE)
MessageBox(NULL, L"HIT", L"",0);
}
return 0;
}


In above code, return value of dwStatusCode from WinHttpQueryHeaders() is 407 when username-password is set for proxy server. Here
WinHttpSetCredentials() is used to provide username and password but still return value of dwStatusCode is 407. If proxy server is
not having username-password then above code works as per requirement i.e., return value of dwStatusCode is 200. But in case of authentication this code fails.



I am using CCProxy version 8.0 for this sample. Please guide with proper solution.










share|improve this question

























  • You don't check return value of WinHttpSetCredentials(). If it returns FALSE, call GetLastError() to get extended error information.

    – zett42
    Nov 16 '18 at 14:07











  • With that have you run it successfully? Because in above code I didn't receive any error. I have checked using 'GetLastError()' api. If you have not run code please don't post any comment. I am looking for running solution.

    – kunal waghmare
    Nov 19 '18 at 5:45




















-2















#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#pragma comment(lib, "winhttp.lib")
int main()
{
static BOOL bRet = FALSE;
TCHAR szHostName[MAX_PATH] = L"www.google.com";
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
const TCHAR BROWSER_INFO = L"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
const WCHAR *WWIZHTTP_TYPES = { L"Accept: image/gif", L"image/x-xbitmap", L"image/jpeg", L"image/pjpeg", L"application/x-shockwave-flash", L"application/x-ms-application", L"application/x-ms-xbap", L"application/vnd.ms-xpsdocument", L"application/xaml+xml", L"application/msword", L"application/vnd.ms-excel", L"application/x-cabinet-win32-x86", L"application/x-pe-win32-x86", L"application/octet-stream", L"application/x-setupscript", L"*//*", NULL };

hSession = WinHttpOpen(BROWSER_INFO, WINHTTP_ACCESS_TYPE_NAMED_PROXY, L"172.168.1.196:808", L"<local>", 0);

if (hSession)
{
hConnect = WinHttpConnect(hSession, szHostName, INTERNET_DEFAULT_HTTP_PORT, 0);
}
if (hConnect)
{
hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL, NULL, WINHTTP_NO_REFERER, WWIZHTTP_TYPES, 0);
}
if (hRequest)
{

//provide username and password

WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_PROXY, WINHTTP_AUTH_SCHEME_BASIC, L"abc", L"abc", NULL);

// Send a request.
bRet = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);

// End the request.
if (bRet)
bRet = WinHttpReceiveResponse(hRequest, NULL);

if (bRet)
{
DWORD dwStatusCode = 0;
DWORD dwTemp = sizeof(dwStatusCode);
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatusCode, &dwTemp, NULL);
if ((dwStatusCode >= HTTP_STATUS_CONTINUE) && (dwStatusCode < HTTP_STATUS_BAD_REQUEST))
{
bRet = TRUE;
}
else
{
bRet = FALSE;
}
}
if (bRet == TRUE)
MessageBox(NULL, L"HIT", L"",0);
}
return 0;
}


In above code, return value of dwStatusCode from WinHttpQueryHeaders() is 407 when username-password is set for proxy server. Here
WinHttpSetCredentials() is used to provide username and password but still return value of dwStatusCode is 407. If proxy server is
not having username-password then above code works as per requirement i.e., return value of dwStatusCode is 200. But in case of authentication this code fails.



I am using CCProxy version 8.0 for this sample. Please guide with proper solution.










share|improve this question

























  • You don't check return value of WinHttpSetCredentials(). If it returns FALSE, call GetLastError() to get extended error information.

    – zett42
    Nov 16 '18 at 14:07











  • With that have you run it successfully? Because in above code I didn't receive any error. I have checked using 'GetLastError()' api. If you have not run code please don't post any comment. I am looking for running solution.

    – kunal waghmare
    Nov 19 '18 at 5:45
















-2












-2








-2


0






#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#pragma comment(lib, "winhttp.lib")
int main()
{
static BOOL bRet = FALSE;
TCHAR szHostName[MAX_PATH] = L"www.google.com";
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
const TCHAR BROWSER_INFO = L"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
const WCHAR *WWIZHTTP_TYPES = { L"Accept: image/gif", L"image/x-xbitmap", L"image/jpeg", L"image/pjpeg", L"application/x-shockwave-flash", L"application/x-ms-application", L"application/x-ms-xbap", L"application/vnd.ms-xpsdocument", L"application/xaml+xml", L"application/msword", L"application/vnd.ms-excel", L"application/x-cabinet-win32-x86", L"application/x-pe-win32-x86", L"application/octet-stream", L"application/x-setupscript", L"*//*", NULL };

hSession = WinHttpOpen(BROWSER_INFO, WINHTTP_ACCESS_TYPE_NAMED_PROXY, L"172.168.1.196:808", L"<local>", 0);

if (hSession)
{
hConnect = WinHttpConnect(hSession, szHostName, INTERNET_DEFAULT_HTTP_PORT, 0);
}
if (hConnect)
{
hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL, NULL, WINHTTP_NO_REFERER, WWIZHTTP_TYPES, 0);
}
if (hRequest)
{

//provide username and password

WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_PROXY, WINHTTP_AUTH_SCHEME_BASIC, L"abc", L"abc", NULL);

// Send a request.
bRet = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);

// End the request.
if (bRet)
bRet = WinHttpReceiveResponse(hRequest, NULL);

if (bRet)
{
DWORD dwStatusCode = 0;
DWORD dwTemp = sizeof(dwStatusCode);
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatusCode, &dwTemp, NULL);
if ((dwStatusCode >= HTTP_STATUS_CONTINUE) && (dwStatusCode < HTTP_STATUS_BAD_REQUEST))
{
bRet = TRUE;
}
else
{
bRet = FALSE;
}
}
if (bRet == TRUE)
MessageBox(NULL, L"HIT", L"",0);
}
return 0;
}


In above code, return value of dwStatusCode from WinHttpQueryHeaders() is 407 when username-password is set for proxy server. Here
WinHttpSetCredentials() is used to provide username and password but still return value of dwStatusCode is 407. If proxy server is
not having username-password then above code works as per requirement i.e., return value of dwStatusCode is 200. But in case of authentication this code fails.



I am using CCProxy version 8.0 for this sample. Please guide with proper solution.










share|improve this question
















#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#pragma comment(lib, "winhttp.lib")
int main()
{
static BOOL bRet = FALSE;
TCHAR szHostName[MAX_PATH] = L"www.google.com";
HINTERNET hSession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;
const TCHAR BROWSER_INFO = L"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 1.1.4322; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
const WCHAR *WWIZHTTP_TYPES = { L"Accept: image/gif", L"image/x-xbitmap", L"image/jpeg", L"image/pjpeg", L"application/x-shockwave-flash", L"application/x-ms-application", L"application/x-ms-xbap", L"application/vnd.ms-xpsdocument", L"application/xaml+xml", L"application/msword", L"application/vnd.ms-excel", L"application/x-cabinet-win32-x86", L"application/x-pe-win32-x86", L"application/octet-stream", L"application/x-setupscript", L"*//*", NULL };

hSession = WinHttpOpen(BROWSER_INFO, WINHTTP_ACCESS_TYPE_NAMED_PROXY, L"172.168.1.196:808", L"<local>", 0);

if (hSession)
{
hConnect = WinHttpConnect(hSession, szHostName, INTERNET_DEFAULT_HTTP_PORT, 0);
}
if (hConnect)
{
hRequest = WinHttpOpenRequest(hConnect, L"GET", NULL, NULL, WINHTTP_NO_REFERER, WWIZHTTP_TYPES, 0);
}
if (hRequest)
{

//provide username and password

WinHttpSetCredentials(hRequest, WINHTTP_AUTH_TARGET_PROXY, WINHTTP_AUTH_SCHEME_BASIC, L"abc", L"abc", NULL);

// Send a request.
bRet = WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);

// End the request.
if (bRet)
bRet = WinHttpReceiveResponse(hRequest, NULL);

if (bRet)
{
DWORD dwStatusCode = 0;
DWORD dwTemp = sizeof(dwStatusCode);
WinHttpQueryHeaders(hRequest, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &dwStatusCode, &dwTemp, NULL);
if ((dwStatusCode >= HTTP_STATUS_CONTINUE) && (dwStatusCode < HTTP_STATUS_BAD_REQUEST))
{
bRet = TRUE;
}
else
{
bRet = FALSE;
}
}
if (bRet == TRUE)
MessageBox(NULL, L"HIT", L"",0);
}
return 0;
}


In above code, return value of dwStatusCode from WinHttpQueryHeaders() is 407 when username-password is set for proxy server. Here
WinHttpSetCredentials() is used to provide username and password but still return value of dwStatusCode is 407. If proxy server is
not having username-password then above code works as per requirement i.e., return value of dwStatusCode is 200. But in case of authentication this code fails.



I am using CCProxy version 8.0 for this sample. Please guide with proper solution.







c++ windows winapi visual-c++ winhttp






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:51







kunal waghmare

















asked Nov 16 '18 at 11:54









kunal waghmarekunal waghmare

12




12













  • You don't check return value of WinHttpSetCredentials(). If it returns FALSE, call GetLastError() to get extended error information.

    – zett42
    Nov 16 '18 at 14:07











  • With that have you run it successfully? Because in above code I didn't receive any error. I have checked using 'GetLastError()' api. If you have not run code please don't post any comment. I am looking for running solution.

    – kunal waghmare
    Nov 19 '18 at 5:45





















  • You don't check return value of WinHttpSetCredentials(). If it returns FALSE, call GetLastError() to get extended error information.

    – zett42
    Nov 16 '18 at 14:07











  • With that have you run it successfully? Because in above code I didn't receive any error. I have checked using 'GetLastError()' api. If you have not run code please don't post any comment. I am looking for running solution.

    – kunal waghmare
    Nov 19 '18 at 5:45



















You don't check return value of WinHttpSetCredentials(). If it returns FALSE, call GetLastError() to get extended error information.

– zett42
Nov 16 '18 at 14:07





You don't check return value of WinHttpSetCredentials(). If it returns FALSE, call GetLastError() to get extended error information.

– zett42
Nov 16 '18 at 14:07













With that have you run it successfully? Because in above code I didn't receive any error. I have checked using 'GetLastError()' api. If you have not run code please don't post any comment. I am looking for running solution.

– kunal waghmare
Nov 19 '18 at 5:45







With that have you run it successfully? Because in above code I didn't receive any error. I have checked using 'GetLastError()' api. If you have not run code please don't post any comment. I am looking for running solution.

– kunal waghmare
Nov 19 '18 at 5:45














1 Answer
1






active

oldest

votes


















0














Please pay attention to the example in this document, If a proxy authentication challenge was responded to, reset those credentials before each SendRequest, because the proxy may require re-authentication after responding.



And if still not working, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication,
example:



if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_USERNAME, proxy_username, proxy_username_len)) 
{
//do error operation.
}
if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_PASSWORD, proxy_password, proxy_password_len))
{
//do error operation.
}





share|improve this answer


























  • still not working

    – kunal waghmare
    Nov 22 '18 at 12:55











  • @kunal waghmare, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication according to MSDN document. I have added an example in my answer.

    – Drake Wu - MSFT
    Nov 23 '18 at 3:00












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337387%2fin-case-of-proxy-server-winhttp-authentication-fails%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Please pay attention to the example in this document, If a proxy authentication challenge was responded to, reset those credentials before each SendRequest, because the proxy may require re-authentication after responding.



And if still not working, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication,
example:



if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_USERNAME, proxy_username, proxy_username_len)) 
{
//do error operation.
}
if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_PASSWORD, proxy_password, proxy_password_len))
{
//do error operation.
}





share|improve this answer


























  • still not working

    – kunal waghmare
    Nov 22 '18 at 12:55











  • @kunal waghmare, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication according to MSDN document. I have added an example in my answer.

    – Drake Wu - MSFT
    Nov 23 '18 at 3:00
















0














Please pay attention to the example in this document, If a proxy authentication challenge was responded to, reset those credentials before each SendRequest, because the proxy may require re-authentication after responding.



And if still not working, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication,
example:



if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_USERNAME, proxy_username, proxy_username_len)) 
{
//do error operation.
}
if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_PASSWORD, proxy_password, proxy_password_len))
{
//do error operation.
}





share|improve this answer


























  • still not working

    – kunal waghmare
    Nov 22 '18 at 12:55











  • @kunal waghmare, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication according to MSDN document. I have added an example in my answer.

    – Drake Wu - MSFT
    Nov 23 '18 at 3:00














0












0








0







Please pay attention to the example in this document, If a proxy authentication challenge was responded to, reset those credentials before each SendRequest, because the proxy may require re-authentication after responding.



And if still not working, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication,
example:



if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_USERNAME, proxy_username, proxy_username_len)) 
{
//do error operation.
}
if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_PASSWORD, proxy_password, proxy_password_len))
{
//do error operation.
}





share|improve this answer















Please pay attention to the example in this document, If a proxy authentication challenge was responded to, reset those credentials before each SendRequest, because the proxy may require re-authentication after responding.



And if still not working, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication,
example:



if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_USERNAME, proxy_username, proxy_username_len)) 
{
//do error operation.
}
if (!WinHttpSetOption(session, WINHTTP_OPTION_PROXY_PASSWORD, proxy_password, proxy_password_len))
{
//do error operation.
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 2:58

























answered Nov 21 '18 at 8:19









Drake Wu - MSFTDrake Wu - MSFT

73819




73819













  • still not working

    – kunal waghmare
    Nov 22 '18 at 12:55











  • @kunal waghmare, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication according to MSDN document. I have added an example in my answer.

    – Drake Wu - MSFT
    Nov 23 '18 at 3:00



















  • still not working

    – kunal waghmare
    Nov 22 '18 at 12:55











  • @kunal waghmare, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication according to MSDN document. I have added an example in my answer.

    – Drake Wu - MSFT
    Nov 23 '18 at 3:00

















still not working

– kunal waghmare
Nov 22 '18 at 12:55





still not working

– kunal waghmare
Nov 22 '18 at 12:55













@kunal waghmare, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication according to MSDN document. I have added an example in my answer.

– Drake Wu - MSFT
Nov 23 '18 at 3:00





@kunal waghmare, you may need to use WinHttpSetOption instead of WinHttpSetCredentials to set proxy authentication according to MSDN document. I have added an example in my answer.

– Drake Wu - MSFT
Nov 23 '18 at 3:00




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53337387%2fin-case-of-proxy-server-winhttp-authentication-fails%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues