简体   繁体   中英

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? I would like to do everything through proxy

API that you use seems to be InternetExplorer derived. It seems that you cannot use proxy's (other than system-wide) using only this API. If that's fine with you, just set your ControlPanel proxy to desired settings and you are good to go. 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.

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. WinINet version will be lengthier and more troublesome I expect. See the MSDN WinINet documentation , specifically InternetSetOption .

EDIT:

I see that you will probably be able to use it to set IE settings both globally and on per-process basis. See How to programmatically query and set proxy settings under Internet Explorer , and documentation for INTERNET_OPTION_PER_CONNECTION_OPTION option.

To do it system-wide just use NULL as hInternet in InternetSetOption . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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