Winet API 支持HTTPP/SOCKS代理
4、下面是程序源码:
void GetLastErrorMessage(DWORD dwError, LPTSTR lpBuffer, DWORD nSize)
{
if (lpBuffer == NULL || nSize == 0)
return;
HLOCAL hLocal = NULL;
BOOL rlt = ::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL, dwError, LANG_NEUTRAL, (LPTSTR)&hLocal, 0, NULL);
if (!rlt)
{
HMODULE hDll = ::LoadLibraryEx(_T("netmsg.dll"), NULL, DONT_RESOLVE_DLL_REFERENCES);
if (hDll != NULL)
{
::FormatMessage(FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM,
hDll, dwError, LANG_NEUTRAL, (LPTSTR)&hLocal, 0, NULL);
::FreeLibrary(hDll);
}
}
//FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,
//NULL, dwError, LANG_NEUTRAL, (PTSTR)&hLocal, 0, NULL);
if (hLocal)
{
lpBuffer[nSize-1] = 0;
_tcsncpy(lpBuffer, (LPTSTR)::LocalLock(hLocal), nSize - 1);
::LocalFree(hLocal);
}
else
{
_tcscpy(lpBuffer, _T("没找到对应的错误码信息"));
}
}
BOOL ParseURL(const char* lpszURL, char* lpszServer, int& Port, char* lpszObject)
{
if (lpszURL == NULL || lpszServer == NULL || lpszObject == NULL)
{
return FALSE;
}
char szCanonicalizeUrl[INTERNET_MAX_URL_LENGTH] = {0};
DWORD dwCanonicalizeUrlLength = INTERNET_MAX_URL_LENGTH;
BOOL bRet = ::InternetCanonicalizeUrlA(lpszURL, szCanonicalizeUrl, &dwCanonicalizeUrlLength, ICU_BROWSER_MODE);
if (bRet)
{
URL_COMPONENTSA Components;
::memset(&Components, 0, sizeof(Components));
Components.dwStructSize = sizeof(Components);
Components.lpszHostName = lpszServer ;
Components.dwHostNameLength = INTERNET_MAX_URL_LENGTH;
Components.lpszUrlPath = lpszObject;
Components.dwUrlPathLength = INTERNET_MAX_URL_LENGTH;
::InternetCrackUrl(szCanonicalizeUrl, dwCanonicalizeUrlLength, 0, &Components);
Port = Components.nPort;
}
return bRet;
}
DWORD DoInternetProxy(LPCTSTR lpszUrl, LPCTSTR lpszProxy)
{
/*The following example code shows how authentication
could be handled using InternetSetOption. */
DWORDdwError = 0;
HINTERNET hOpenHandle, hConnectHandle, hResourceHandle;
hOpenHandle = InternetOpen("DoInternetProxy",
INTERNET_OPEN_TYPE_PRECONFIG,
NULL, NULL, 0);
if (hOpenHandle)
{
if (lpszProxy && lpszProxy[0])
{
printf("设置代理%s\n", lpszProxy);
/*INTERNET_PER_CONN_OPTION_LIST List;
INTERNET_PER_CONN_OPTION Option[1];
unsigned long nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
Option[0].dwOption = INTERNET_PER_CONN_PROXY_SERVER;
Option[0].Value.pszValue = (char*)lpszProxy;
List.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
List.pszConnection = NULL;
List.dwOptionCount = 1;
List.dwOptionError = 0;
List.pOptions = Option;
if(InternetSetOption(hOpenHandle, INTERNET_OPTION_PER_CONNECTION_OPTION, &List, nSize))*/
INTERNET_PROXY_INFO ProxyInfo = {0};
ProxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
ProxyInfo.lpszProxy = lpszProxy;
ProxyInfo.lpszProxyBypass = NULL;
if(InternetSetOption(hOpenHandle, INTERNET_OPTION_PROXY, &ProxyInfo, sizeof(ProxyInfo)))
{
/*char szYN[64];
printf("是否输入代理用户名/密码(输入Y/N)?");
scanf("%s", szYN);
if (stricmp(szYN, "Y") == 0)
{
char szUsername[64] = {0}, szPassword[64] = {0};
printf("请输入代理用户名:");
scanf("%s", szUsername);
printf("请输入代理用户密码:");
scanf("%s", szPassword);
InternetSetOption(hOpenHandle,
INTERNET_OPTION_PROXY_USERNAME,
szUsername,
strlen(szUsername)+1);
InternetSetOption(hOpenHandle,
INTERNET_OPTION_PROXY_PASSWORD,
szPassword,
strlen(szPassword)+1);
}*/
}
else
{
printf("设置代理失败!\n");
dwError = GetLastError();
}
}
char szServer[INTERNET_MAX_URL_LENGTH] = {0};
int nPort = 80;
char szObject[INTERNET_MAX_URL_LENGTH] = {0};
if(ParseURL(lpszUrl, szServer, nPort, szObject))
{
printf("测试URL:%s:%d %s\n", szServer, nPort, szObject);
hConnectHandle = InternetConnect(hOpenHandle,
szServer,
nPort,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,0);
if (hConnectHandle)
{
hResourceHandle = HttpOpenRequest(hConnectHandle,
"GET",
szObject,
NULL,
NULL,
NULL,
INTERNET_FLAG_KEEP_CONNECTION,
0);
if (hResourceHandle)
{
do
{
if(HttpSendRequest(hResourceHandle,
NULL,
0,
NULL,
0))
{
DWORDdwStatus = 0;
DWORDdwStatusSize = sizeof(dwStatus);
charszUsername[64] = {0}, szPassword[64] = {0};
HttpQueryInfo(hResourceHandle,
HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE,
&dwStatus, &dwStatusSize,
NULL);
if(dwStatus == HTTP_STATUS_PROXY_AUTH_REQ)
{// Proxy Authentication Required
// Insert code to set strUsername and strPassword.
// cchUserLength is the length of strUsername and
// cchPasswordLength is the length of strPassword.
printf("请输入代理用户名:");
scanf("%s", szUsername);
printf("请输入代理用户密码:");
scanf("%s", szPassword);
// Insert code to safely determine cchUserLength and
// cchPasswordLength. Insert appropriate error handling code.
InternetSetOption(hResourceHandle,
INTERNET_OPTION_PROXY_USERNAME,
szUsername,
strlen(szUsername)+1);
InternetSetOption(hResourceHandle,
INTERNET_OPTION_PROXY_PASSWORD,
szPassword,
strlen(szPassword)+1);
continue;
}
if(dwStatus == HTTP_STATUS_DENIED)
{// Server Authentication Required.
// Insert code to set strUsername and strPassword.
// cchUserLength is the length of strUsername and
// cchPasswordLength is the length of strPassword.
printf("请输入%s用户名:", szServer);
scanf("%s", szUsername);
printf("请输入%s用户密码:", szServer);
scanf("%s", szPassword);
// Insert code to safely determine cchUserLength and
// cchPasswordLength. Insert error handling code as
// appropriate.
InternetSetOption(hResourceHandle,
INTERNET_OPTION_USERNAME,
szUsername,
strlen(szUsername)+1);
InternetSetOption(hResourceHandle,
INTERNET_OPTION_PASSWORD,
szPassword,
strlen(szPassword)+1);
continue;
}
}
else
{
dwError = GetLastError();
}
break;
} while (1);
// Insert code to read the data from the hResourceHandle
// at this point.
if (dwError == 0)
{
DWORD dwContentLength = 0;
DWORD dwContentLengthSize = sizeof(dwContentLengthSize);
if(!HttpQueryInfo( hResourceHandle, HTTP_QUERY_CONTENT_LENGTH|HTTP_QUERY_FLAG_NUMBER, (LPVOID)&dwContentLength, &dwContentLengthSize, NULL))
{
dwContentLength = 0;
}
printf("内容长度[%d]\n", dwContentLength);
char szResource[256] = {0};
DWORD dwResourceLength = 255;
if(!InternetReadFile( hResourceHandle, szResource, dwResourceLength, &dwResourceLength))
{
printf("InternetReadFile失败!\n");
dwError = GetLastError();
}
else
{
printf("%s%s\n", szResource, dwResourceLength<dwContentLength?"......":"");
}
}
}
else
{
printf("HttpOpenRequest失败!\n");
dwError = GetLastError();
}
}
else
{
printf("InternetConnect失败!\n");
dwError = GetLastError();
}
}
else
{
printf("解析%s地址失败!\n", lpszUrl);
dwError = GetLastError();
}
//重置代理
//INTERNET_PROXY_INFO ResetProxyInfo = {0};
//InternetSetOption(hOpenHandle, INTERNET_OPTION_PROXY, &ResetProxyInfo, sizeof(ResetProxyInfo));
}
else
{
printf("InternetOpen失败!\n");
dwError = GetLastError();
}
if(hResourceHandle)
{
InternetCloseHandle(hResourceHandle);
hResourceHandle = NULL;
}
if(hConnectHandle)
{
InternetCloseHandle(hConnectHandle);
hConnectHandle = NULL;
}
if (hOpenHandle)
{
InternetCloseHandle(hOpenHandle);
hOpenHandle = NULL;
}
return dwError;
}
int main(int argc, char* argv[])
{
//printf("Hello World!\n");
HANDLE hStdin = GetStdHandle( STD_OUTPUT_HANDLE );
SetStdHandle( STD_OUTPUT_HANDLE, (void *)0x7 );
char szBuffer[2048] = {0};
long flag = 1; //0 退出
do
{
SetConsoleTextAttribute( (void *)0x7, FOREGROUND_INTENSITY | FOREGROUND_INTENSITY );
/*if (argc < 2
|| strcmp(argv[1],"help") == 0
|| strcmp(argv[1],"?") == 0)*/
{
printf("\n====================代理格式====================\n");
printf("[<protocol>=][<scheme>://]<proxyserver>[:<port>]\n");
printf("eg:\n");
printf("proxyserver:port(默认HTTP代理)\n");
printf("FTP=ftp://proxyserver:port\n");
printf("SOCKS=proxyserver:port\n");
printf("\n================================================\n");
}
SetConsoleTextAttribute( (void *)0x7, FOREGROUND_INTENSITY | FOREGROUND_GREEN );
printf("请输入代理地址:(参考代理格式,输入任意字符换行表示不使用代理)\n");
char szProxy[INTERNET_MAX_URL_LENGTH] = {0};
memset(szBuffer, 0, sizeof(szBuffer));
scanf("%s", szBuffer);
if (strlen(szBuffer) >= 8)
{
strcpy(szProxy, szBuffer);
}
do
{
printf("请输入测试URL,例如:http://www.baidu.com:80/),输入exit退出,reset重新设置代理\n");
char szUrl[INTERNET_MAX_URL_LENGTH] = {0};
memset(szBuffer, 0, sizeof(szBuffer));
scanf("%s", szBuffer);
if (stricmp(szBuffer, "exit") == 0)
{
flag = 0;
break;
}
if (stricmp(szBuffer, "reset") == 0)
{
flag = 1;
break;
}
if (strnicmp(szBuffer, "http://", strlen("http://")) != 0)
{
strcpy(szUrl, "http://");
}
strcat(szUrl, szBuffer);
DWORD ret = DoInternetProxy(szUrl, szProxy);
if (ret)
{
SetConsoleTextAttribute( (void *)0x7, FOREGROUND_INTENSITY | FOREGROUND_RED );
GetLastErrorMessage(ret, szBuffer, 2048);
printf("测试代理完成[%d]%s!\n", ret, szBuffer);
SetConsoleTextAttribute( (void *)0x7, FOREGROUND_INTENSITY | FOREGROUND_GREEN );
}
else
{
printf("测试代理完成!\n");
}
} while (1);
} while (flag);
SetStdHandle( STD_OUTPUT_HANDLE, hStdin );
//system("pause");
return 0;
}
Winet API 支持HTTPP/SOCKS代理的更多相关文章
- 【转载】SOCKS代理:从***到内网漫游
原文:SOCKS代理:从***到内网漫游 本文原创作者:tahf,本文属FreeBuf原创奖励计划,未经许可禁止转载 之前在Freebuf上学习过很多大牛写的关于Tunnel.SOCKS代理.***等 ...
- Mac Aria2 使用Privoxy将socks代理转化为http代理
安装Privoxy 打开终端安装privoxy来实现这里我是通过brew来进行的安装 brew install privoxy 看到这行已经安装成功 ==> Caveats To have la ...
- 使用ssh正向连接、反向连接、做socks代理的方法
ssh -L 219.143.16.157:58080:172.21.163.32:8080 用户名@localhost -p 10142 在 219.143.16.157机器执行 将ssh隧 ...
- 给HttpClient添加Socks代理
本文描述http client使用socks代理过程中需要注意的几个方面:1,socks5支持用户密码授权:2,支持https:3,支持让代理服务器解析DNS: 使用代理创建Socket 从原理上来看 ...
- linux配置wifi连接并通过ssh代理开启socks代理
1, 命令行配置连接wifi具体我是用的cubieboard2上Debian主机,其中配置wifi的命令行有wpa_cli,具体用法步骤如下.wpa_cli 命令行执行需要root权限,详细用法请见 ...
- Xshell添加ssh隧道SOCKS代理
Xshell是一个功能强大的终端模拟器,支持SSH,SFTP.TELNET.RLOGIN和SERIAL 下载地址:http://www.netsarang.com/products/xsh_overv ...
- 给OkHttp Client添加socks代理
Okhttp的使用没有httpClient广泛,网上关于Okhttp设置代理的方法很少,这篇文章完整介绍了需要注意的方方面面. 上一篇博客中介绍了socks代理的入口是创建java.net.Socke ...
- JAVA知识积累 给HttpClient添加Socks代理
本文描述http client使用socks代理过程中需要注意的几个方面:1,socks5支持用户密码授权:2,支持https:3,支持让代理服务器解析DNS: 使用代理创建Socket 从原理上来看 ...
- steam linux 使用socks代理
环境:Ubuntu 15.10 64bit,Steam:built:May 10 2016 需要的工具:ssh/shadowsocks等socks5代理,tsocks ---------------- ...
随机推荐
- DataGridView插入一行数据和用DataTable绑定数据2种方式
以前不会用DataGridView的时候一直使用DataTable绑定的方式,如下: DataTable table = new DataTable(); //给表添加一列Name,此名字和 tabl ...
- Responsive Design响应式网站设计心得笔记
这个词已经喊了很久了,一直都是小打小闹,没正经的做过大的响应式全站,这次终于有机会了.网站刚上线半个月,就要改版为响应式设计,支持手机/PC等各类终端显示浏览.今天把首页做好,并测试无误,这里把一些应 ...
- JavaScript版排序算法
JavaScript版排序算法:冒泡排序.快速排序.插入排序.希尔排序(小数据时,希尔排序会比快排快哦) //排序算法 window.onload = function(){ var array = ...
- [C#]中英文字幕合并的小程序
今天班里小组合作录了一个视频,我给它做了字幕的时间轴.为了让这个视频假装很高端的样子,我想再加上英文的字幕.中文的纯字幕文本先搞成一句一行,然后放到Google翻译上,复制英文保存在Eng.txt. ...
- MySQL 关闭子表的外键约束检察
准备: 定义一个教师表.一个学生表:在学生表中引用教师表ID create table teachers(teacherID int not null auto_increment primary k ...
- IBM HeapAnalyzer
https://www.ibm.com/developerworks/community/wikis/home?lang=en#!/wiki/W3b463571efc8_4f02_99af_3cbc0 ...
- Windows 键盘快捷键
Windows 键盘快捷键 标签页和窗口快捷键 Ctrl+N 打开新窗口. Ctrl+T 打开新标签页. Ctrl+Shift+N 在隐身模式下打开新窗口. 按 Ctrl+O,然后选择文件. 通过 G ...
- 繁简转换OpenCC,autogb 和 autob5,iconv,python的jianfan包
OpenCC OpenCC 是跨平台.多语言的开放中文转换库,除了基本的简繁转换功能外,用户还可以选择对不同用词习惯和异体字的处理方式. OpenCC 还提供方便的网页转换界面. OpenOffice ...
- Microsoft Azure 上的自定义数据和 Cloud-Init
自定义数据是什么? 客户经常询问如何才能在配置Microsoft Azure 虚拟机时插入脚本或其他元数据.在其他云中,这个概念通常称为用户数据.MicrosoftAzure 中也有一项类似的功 ...
- GDI泄露检测
前一段在一个仿QQ的IM通讯工具中加入屏幕截图以及截图编辑功能,但是测试中发现当连续进行几十次截图后,系统会出现白屏,开始以为和win7经常闪白有关,屏幕截图截取到闪白的瞬间导致白屏,后来发现是GDI ...