package Microsoft.Samples.QuickStart.HowTo.Net.WebRequests;
//-----------------------------------------------------------------------
//  This file is part of the Microsoft .NET Framework SDK Code Samples.
// 
//  Copyright (C) Microsoft Corporation.  All rights reserved.
// 
//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.
// 
//THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
import System.*;
import System.Net.*;
import System.IO.*;
import System.Text.*;

final class ClientGetWithProxy
{
    private static boolean bShow;

    public static void main(String[] args)
    {
        if (args.get_Length() < 2) {
            ShowUsage();
        }
        else {
            if (args.get_Length() > 2) {
                bShow = false;
            }
            else {
                bShow = true;
            }
            GetPage(args[0], args[1]);
        }
        Console.WriteLine();
        Console.WriteLine("Press Enter to continue...");
        Console.ReadLine();

        return;
    } //main

    private static void ShowUsage()
    {
        Console.WriteLine("Attempts to GET a URL with a proxy");
        Console.WriteLine();
        Console.WriteLine("Usage:");
        Console.WriteLine("ClientGetWithProxy URL proxyname");
        Console.WriteLine();
        Console.WriteLine("Examples:");
        Console.WriteLine("ClientGetWithProxy http://www.microsoft.com/net/ " 
            + "myproxy");
    } //ShowUsage

    private static void GetPage(String url, String proxy)
    {
        WebResponse response = null;
        StreamReader reader = null;
        try
        {
            WebProxy proxyObject = new WebProxy(proxy, 80);
            // Disable Proxy use when the host is local i.e. without periods.
            proxyObject.set_BypassProxyOnLocal(true);

            // Set the default global proxy
            HttpWebRequest.set_DefaultWebProxy(proxyObject);

            WebRequest request = WebRequest.Create(url);
            response = request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            reader = new StreamReader(responseStream);
            Console.WriteLine("\r\nResponse stream received");
            if (bShow)
            {
                char buffer[] = new char[256];
                int count = reader.Read(buffer, 0, buffer.length);

                Console.WriteLine("HTML...\r\n");
                while (count > 0)
                {
                    Console.Write(new String(buffer, 0, count));
                    count = reader.Read(buffer, 0, buffer.length);
                }
                Console.WriteLine("");
            }
        }
        catch (UriFormatException exp)
        {
            Console.WriteLine("\r\nThe request URI was malformed");
        }
        catch (WebException exp)
        {
            Console.WriteLine("\r\nThe request URI could not be found.");
        }
        catch (IOException exp)
        {
            Console.WriteLine("\r\nThe request URI could not be retrieved");
        }
        finally
        {
            if (response != null)
                response.Close();
            if (reader != null)
                reader.Close();
        }
    } //GetPage
} //ClientGetWithProxy