0

I'm trying to download a file using Tor. I found code which is working, but it's a Windows Forms browser control (=Internet Explorer) while I just want to download a file.

I tried it with a WebRequest (when Tor is running), but it's just loading eternally:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.icanhazip.com/");
request.Proxy = new WebProxy("127.0.0.1", 8182);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
    MessageBox.Show(new StreamReader(response.GetResponseStream()).ReadToEnd());
}

But the code I found using the same IP and port for a browser control works. Unfortunally the code for a proxy there is more complex:

// (uses wininet.dll)
IntPtr hInternet = InternetOpen(agentName, INTERNET_OPEN_TYPE_DIRECT, null, null, 0);

INTERNET_PER_CONN_OPTION[] options = new INTERNET_PER_CONN_OPTION[2];
options[0] = new INTERNET_PER_CONN_OPTION();
options[0].dwOption = (int)INTERNET_PER_CONN_OPTIONENUM.INTERNET_PER_CONN_FLAGS;
options[0].value.dwValue = (int)INTERNET_OPTION_PER_CONN_FLAGS.PROXY_TYPE_PROXY;

options[1] = new INTERNET_PER_CONN_OPTION();
options[1].dwOption = (int)INTERNET_PER_CONN_OPTIONENUM.INTERNET_PER_CONN_PROXY_SERVER;
options[1].value.pszValue = Marshal.StringToHGlobalAnsi("127.0.0.1:8182");

IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(options[0]) + Marshal.SizeOf(options[1]));
IntPtr current = buffer;

for (int i = 0; i < options.Length; i++)
{
    Marshal.StructureToPtr(options[i], current, false);
    current = (IntPtr)((int)current + Marshal.SizeOf(options[i]));
}

INTERNET_PER_CONN_OPTION_LIST optionList = new INTERNET_PER_CONN_OPTION_LIST();
optionList.pOptions = buffer;
optionList.Size = Marshal.SizeOf(optionList);
optionList.Connection = IntPtr.Zero;
optionList.OptionCount = options.Length;
optionList.OptionError = 0;

int size = Marshal.SizeOf(optionList);
IntPtr intPtrStruct = Marshal.AllocCoTaskMem(size);
Marshal.StructureToPtr(optionList, intPtrStruct, true);

bool bReturn = InternetSetOption(hInternet, INTERNET_OPTION.INTERNET_OPTION_PER_CONNECTION_OPTION, intPtrStruct, size);

Marshal.FreeCoTaskMem(buffer);
Marshal.FreeCoTaskMem(intPtrStruct);

InternetCloseHandle(hInternet);

This uses a library (Tor.NET) which is supposed to make it possible to use tor as proxy. I wrote my code in the same file right after the working code.

Why does this work but my WebRequest doesn't? What's the difference?

user3817008
  • 613
  • 7
  • 13
  • Tor is a SocksWebProxy, one you address through WinInet. A SocksProxy at Github [SocksWebProxy](https://github.com/postworthy/SocksWebProxy/tree/master/SocksWebProxy). – Jimi Dec 09 '17 at 16:18
  • Possible duplicate of [using Tor as Proxy](https://stackoverflow.com/questions/1962483/using-tor-as-proxy) – Jimi Dec 09 '17 at 16:18
  • Thank you for your answer. The working example uses a library where I guess that it enables a Socks proxy. I wrote my code right behind the working code. I think that WinInet doesn't support Socks on its own. – user3817008 Dec 09 '17 at 17:02
  • 1
    Of course it does, it's WinHTTP that does not. This is a good reading [Porting WinINet Applications to WinHTTP](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384068(v=vs.85).aspx) – Jimi Dec 09 '17 at 17:11

0 Answers0