Search code examples
c#.nethttphttprequesttor

DotNetTor Cannot Make Requests


Recently I want to get a .json content from an onion site (e.g. http://takedownmi4lfjhv.onion/root.json, this is an existing link)

So I installed the C# library DotNetTor (Nuget package is here)

I copy & pasted the DotNetTor QuickStart Example Project sample code and ran it with my C# project.

        var requestUri = "http://icanhazip.com/";

        // 1. Get real IP
        using (var httpClient = new HttpClient())
        {
            var message = httpClient.GetAsync(requestUri).Result;
            var content = message.Content.ReadAsStringAsync().Result;
            Console.WriteLine($"Your real IP: \t\t{content}");
        }

        // 2. Get TOR IP
        using (var httpClient = new HttpClient(new SocksPortHandler("127.0.0.1", socksPort: 9050)))
        {
            var message = httpClient.GetAsync(requestUri).Result; // GOT ERROR HERE // GOT ERROR HERE
            var content = message.Content.ReadAsStringAsync().Result;
            Console.WriteLine($"Your TOR IP: \t\t{content}");

            // 3. Change TOR IP
            var controlPortClient = new DotNetTor.ControlPort.Client("127.0.0.1", controlPort: 9051, password: "ILoveBitcoin21");
            controlPortClient.ChangeCircuitAsync().Wait();

            // 4. Get changed TOR IP
            message = httpClient.GetAsync(requestUri).Result;
            content = message.Content.ReadAsStringAsync().Result;
            Console.WriteLine($"Your other TOR IP: \t{content}");
        }

However, at the line I marked // GOT ERROR HERE, it poped up an error

System.AggregateException

Inner Exception 1:
TorException: Failed to send the request

Inner Exception 2:
SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:9050

Why failed to send the request? How to resolve that?


Solution

  • The code above is simply a Socks Proxy handler and does not really contain anything about Tor.

    new SocksPortHandler("127.0.0.1", socksPort: 9050)

    See this link of code. This is nothing more than connecting via a SocksPortHandler 127.0.0.1:9050

    But this port is not yet configured to Tor at all.

    So before this, you should configure the port.

    You can download the Tor Expert Bundle from here. This can help you configure that port for Tor.