问题描述
我正在尝试使用 InternetSetOption(...) 方法为代理设置用户名和密码.但是,它始终返回零,并且最后一个错误设置为 12018.下面是我的代码片段.
I am trying to set user name and password for proxy using InternetSetOption(...) method. However, it always returns zero and Last error is set to 12018.Below is my code snippet.
#include "stdafx.h"
#include <Wininet.h>
int _tmain(int argc, _TCHAR* argv[])
{
HINTERNET _session = ::InternetOpen(_T("TestProgram"),
INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, NULL);
LPCTSTR proxyUserName = L"username";
LPCTSTR proxyPassword = L"userpassword";
BOOL b = ::InternetSetOption(_session,INTERNET_OPTION_PROXY_USERNAME ,(LPVOID)proxyUserName,wcslen(proxyUserName)+1 );
printf(" InternetSetOption returns - %d\n",b);
printf(" InternetSetOption GetLastError - %d\n",GetLastError());
b = ::InternetSetOption(_session,INTERNET_OPTION_PROXY_PASSWORD,(LPVOID)proxyPassword,wcslen(proxyPassword) +1 );
printf(" InternetSetOption returns - %d\n",b);
printf(" InternetSetOption GetLastError - %d\n",GetLastError());
getchar();
return 0;
}
我尝试使用管理员和非管理员权限执行上述程序,但没有成功.欢迎任何帮助.
I tried to execute above program with Admin and non-admin permissions but no luck.Any help is welcome.
谢谢,好听的
推荐答案
您使用了不正确的 HINTERNET
句柄.来自 选项标志文档:
You have used incorrect HINTERNET
handle.From Option Flags documentation:
INTERNET_OPTION_PROXY_USERNAME 这个选项可以在手柄上设置由 InternetConnect 或 HttpOpenRequest 返回.
INTERNET_OPTION_PROXY_PASSWORD 这个选项可以在手柄上设置由 InternetConnect 或 HttpOpenRequest 返回.
INTERNET_OPTION_PROXY_PASSWORD This option can be set on the handle returned by InternetConnect or HttpOpenRequest.
因此,您必须使用 InternetConnect()
或 HttpOpenRequest()
.
So, you must use InternetConnect()
or HttpOpenRequest()
.
这篇关于WinInet::InternetSetOption(...) 总是返回 0 并且 GetLastError() 返回 12018的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!