简体   繁体   English

通过代理的C ++套接字连接

[英]C++ socket connection through proxy

hello guys I ask my question, I have this code that download a web page 大家好,我问我的问题,我有这段代码可以下载网页

#include <iostream>
#include <urlmon.h>
#include <string>
#pragma comment(lib,"urlmon.lib")
using namespace std;
int main()
{
    string indirizzo;
    IStream * is;
    char buffer[256];
    cout<<"Insert adress of the web page: ";
    cin>>indirizzo;
    if(URLOpenBlockingStream(NULL,indirizzo.c_str(),&is,0,NULL)!=S_OK)
    {
        cerr<<"ERROR DOWNLOAD.";
    }
    else
    {
        cout<<"download OK"<<endl;
        system("Pause");
        ULONG readBytes;
        while(is->Read(buffer,sizeof(buffer),&readBytes)==S_OK)
        {
           cout.write(buffer,readBytes);
        }
        is->Release();
    }
    system("cls");
    system("Pause");
    return 0;
}

You can do this by connecting the socket via a proxy before the http request? 您可以通过在HTTP请求之前通过代理连接套接字来做到这一点? I would like to do everything through proxy 我想通过代理做所有事情

API that you use seems to be InternetExplorer derived. 您使用的API似乎是InternetExplorer派生的。 It seems that you cannot use proxy's (other than system-wide) using only this API. 似乎您不能仅使用此API来使用代理服务器(系统范围以外的代理服务器)。 If that's fine with you, just set your ControlPanel proxy to desired settings and you are good to go. 如果您觉得合适,只需将ControlPanel代理设置为所需的设置就可以了。 Browsers other than IE might even get configured so they use they own proxy settings, so you can limit the effects of system-wide settings if you need it only for your program. IE以外的浏览器甚至可能已配置为使用他们自己的代理设置,因此,如果仅需要程序使用,则可以限制系统范围设置的影响。

If you need it stricly only for your app, or otherwise hidden/secured (users might be able to see/change IE settings) you might try to use WinINet to download your files, but that's not what you are asking. 如果仅对您的应用严格需要它,或者以其他方式隐藏/保护(用户可能能够查看/更改IE设置),则可以尝试使用WinINet下载文件,但这不是您要的。 WinINet version will be lengthier and more troublesome I expect. 我期望WinINet版本会更长,也更麻烦。 See the MSDN WinINet documentation , specifically InternetSetOption . 请参阅MSDN WinINet文档 ,尤其是InternetSetOption

EDIT: 编辑:

I see that you will probably be able to use it to set IE settings both globally and on per-process basis. 我看到您可能将可以使用它在全局和每个进程的基础上设置IE设置。 See How to programmatically query and set proxy settings under Internet Explorer , and documentation for INTERNET_OPTION_PER_CONNECTION_OPTION option. 请参阅如何在Internet Explorer下以编程方式查询和设置代理设置 ,以及INTERNET_OPTION_PER_CONNECTION_OPTION选项的文档。

To do it system-wide just use NULL as hInternet in InternetSetOption . 要在系统范围内做到这一点,只需在InternetSetOption hInternet NULL用作hInternet To do it just for your own process, use InternetOpen before any other networking code, and use returned handle - it seems it should also affect dynamically loaded IE engine. 要仅针对您自己的过程执行此操作,请在使用任何其他网络代码之前使用InternetOpen ,并使用返回的句柄-看来它也应该影响动态加载的IE引擎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM