Exemple #1
0
        internal void ConnectHTTPProxy()
        {
            HTTPRequest request = new HTTPRequest("CONNECT "
                                                  + hostname
                                                  + ":"
                                                  + port
                                                  + " HTTP/1.0");

            request.SetHeaderField("User-Agent", "Maverick.NET");
            request.SetHeaderField("Pragma", "No-Cache");
            request.SetHeaderField("Host", hostname);
            request.SetHeaderField("Proxy-Connection", "Keep-Alive");

            byte[] tmp;

            if (username != null)
            {
                tmp = SupportClass.ToByteArray(username + ":" + (password == null ? "" : password));
                request.SetHeaderField("Proxy-Authorization",
                                       "Basic " + Convert.ToBase64String(tmp, 0, tmp.Length));
            }

            tmp = SupportClass.ToByteArray(request.ToString());
            netStream.Write(tmp, 0, tmp.Length);

            HTTPResponse response = new HTTPResponse(netStream);

            if (response.Status == 407)
            {
                throw new IOException("Proxy authentication required method="
                                      + response.AuthenticationMethod
                                      + " realm="
                                      + response.AuthenticationRealm);
            }

            if ((response.Status < 200) || (response.Status > 299))
            {
                throw new IOException("Proxy tunnel setup failed: " +
                                      response.GetStartLine());
            }
        }