Search code examples
c#httprequestsockshttp-status-code-302

HTTP requests through SOCKS-Proxy getting redirected (HTTP 302) C#


Hey guys I have written some code to connect to a SOCKS5-Proxy and handle some HTTP requests, the problem is that every request I'm doing keeps getting redirected (HTTP 302 Response).

My Request code:

try
{
    using (var writer = new StreamWriter(stream))
    {
        writer.Write("GET /\r\n HTTP/1.0\r\n Host: www.google.com\r\n\r\n");
        writer.Flush();
        using (var reader = new StreamReader(stream))
        {
            Char[] readBuff = new Char[256];
            int count = reader.Read(readBuff, 0, 256);

            Console.WriteLine("\nThe HTML contents of page the are  : \n\n ");
            while (count > 0)
            {
                String outputData = new String(readBuff, 0, count);
                Console.WriteLine(outputData);
                count = reader.Read(readBuff, 0, 256);
            }
        }
    }
}
catch (Exception e)
{
    MessageBox.Show(e.Message);
}

I get a 302-response for every website I try. Is this problem due to my request? Or possibly due to the SOCKS-Proxy ?

If you need the code for establishment of SOCKS-Proxy and/or TCP-connection to host just ask. But I thought it was irrelevant.

Thanks!


Solution

  • You have an erroneous line break in your request. The HTTP version needs to be on the same line as the method and resource being requested. Otherwise the proxy/server will think you are an old HTTP 0.9 client instead of an HTTP 1.0 client.

    Change this:

    writer.Write("GET /\r\n HTTP/1.0\r\n Host: www.google.com\r\n\r\n");
    

    To this:

    writer.Write("GET / HTTP/1.0\r\nHost: www.google.com\r\n\r\n");