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?