home *** CD-ROM | disk | FTP | other *** search
/ Chip 2005 June / ccd0605.iso / Software / Shareware / Programare / httpcurrent / HttpGetSample.java next >
Text File  |  2005-04-29  |  9KB  |  279 lines

  1. // Copyright 2002-2003 (c) oakland software, All rights reserved
  2.  
  3. package com.oaklandsw.http.sa;
  4.  
  5. import java.io.InputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.URL;
  8. import java.security.Security;
  9.  
  10. /******************************************************************
  11.  
  12. Sample program for Oakland Software Java HTTP Client
  13.  
  14. usage: java HttpGetSample <url> <options>
  15.  
  16.   url - the URL to get from
  17.   options:
  18.  
  19.     -pass <password> - the password to send
  20.     -user <userName> - the user name to send
  21.     -host <hostName> - the host name to send (NTLM only)
  22.     -dom <domainName> - the domain name to send (NTLM only)
  23.     -pxpass <password> - the password to send to proxy server
  24.     -pxuser <userName> - the user name to send to proxy server
  25.     -pxhost <hostName> - the host name to send to proxy server
  26.     -pxdom <domainName> - the domain name to send to proxy server
  27.     -loop <count> - the number of times to iterate
  28.     -proxy <host:port> - the proxy host/port to use
  29.  
  30.  
  31.  ******************************************************************/
  32.  
  33.  
  34.  
  35.  
  36. public class HttpGetSample implements com.oaklandsw.http.HttpUserAgent
  37. {
  38.  
  39.     public HttpGetSample()
  40.     {
  41.     }
  42.  
  43.  
  44.     private static boolean                          _interactive;
  45.     private static boolean                          _nooutput;
  46.  
  47.     private static boolean                          _dooaklandsw = true;
  48.  
  49.     private static int                              _loopCount;
  50.     
  51.     private static String                           _proxyHost;
  52.     private static int                              _proxyPort;
  53.  
  54.     private static boolean                          _useConnectionProxy;
  55.     
  56.     public static com.oaklandsw.http.NtlmCredential    _normalCredential;
  57.     public static com.oaklandsw.http.NtlmCredential    _proxyCredential;
  58.  
  59.  
  60.     static
  61.     {
  62.         _normalCredential = new com.oaklandsw.http.NtlmCredential();
  63.         _normalCredential.setUser("not set");
  64.         _normalCredential.setPassword("not set");
  65.         _normalCredential.setHost("not set");
  66.         _normalCredential.setDomain("not set");
  67.  
  68.         _proxyCredential = new com.oaklandsw.http.NtlmCredential();
  69.         _proxyCredential.setUser("not set");
  70.         _proxyCredential.setPassword("not set");
  71.         _proxyCredential.setHost("not set");
  72.         _proxyCredential.setDomain("not set");
  73.     }
  74.  
  75.  
  76.     // HttpUserAgent Interface - get credentials for server
  77.     public com.oaklandsw.http.Credential getCredential(String realm,
  78.                                                     String url,
  79.                                                     int scheme)
  80.     {
  81.  
  82.         System.out.println("getGred: " + realm + " url: "  
  83.                    + url + " scheme: " + scheme);
  84.  
  85.         com.oaklandsw.http.NtlmCredential cred = _normalCredential;
  86.         System.out.println("Returning normal cred: " + cred.getUser());
  87.         return cred;
  88.     }
  89.  
  90.  
  91.     // HttpUserAgent Interface - get credentials for proxy
  92.     public com.oaklandsw.http.Credential getProxyCredential(String realm,
  93.                                          String url,
  94.                                          int scheme)
  95.     {
  96.         System.out.println("getProxyGred: " + realm + " url: "  
  97.                    + url + " scheme: " + scheme);
  98.  
  99.         com.oaklandsw.http.NtlmCredential cred = _proxyCredential;
  100.         System.out.println("Returning proxy cred: " + cred.getUser());
  101.         return cred;
  102.     }
  103.  
  104.  
  105.     private static void extractProxy(String hostAndPort)
  106.     {
  107.         int ind = hostAndPort.indexOf(":");
  108.         if (ind > 0)
  109.         {
  110.             _proxyHost = hostAndPort.substring(0, ind);
  111.             try
  112.             {
  113.                 _proxyPort = Integer.parseInt
  114.                     (hostAndPort.substring(ind + 1));
  115.             }
  116.             catch (Exception ex)
  117.             {
  118.                 throw new RuntimeException("Invalid port: "
  119.                                         + hostAndPort);
  120.             }
  121.         }
  122.         else
  123.         {
  124.             _proxyHost = hostAndPort;
  125.             _proxyPort = 80;
  126.         }
  127.     }
  128.     
  129.     public static final void main(String[] args)
  130.         throws Exception
  131.     {
  132.         HttpGetSample userAgent = new HttpGetSample();
  133.  
  134.         _loopCount = 1;
  135.         
  136.         int index = 1;
  137.         while (index < args.length)
  138.         {
  139.             if (args[index].equalsIgnoreCase("-user"))
  140.                 _normalCredential.setUser(args[++index]);
  141.             else if (args[index].equalsIgnoreCase("-pass"))
  142.                 _normalCredential.setPassword(args[++index]);
  143.             else if (args[index].equalsIgnoreCase("-dom"))
  144.                 _normalCredential.setDomain(args[++index]);
  145.             else if (args[index].equalsIgnoreCase("-host"))
  146.                 _normalCredential.setHost(args[++index]);
  147.  
  148.             else if (args[index].equalsIgnoreCase("-pxuser"))
  149.                 _proxyCredential.setUser(args[++index]);
  150.             else if (args[index].equalsIgnoreCase("-pxpass"))
  151.                 _proxyCredential.setPassword(args[++index]);
  152.             else if (args[index].equalsIgnoreCase("-pxdom"))
  153.                 _proxyCredential.setDomain(args[++index]);
  154.             else if (args[index].equalsIgnoreCase("-pxhost"))
  155.                 _proxyCredential.setHost(args[++index]);
  156.             else if (args[index].equalsIgnoreCase("-interactive"))
  157.                 _interactive = true;
  158.             else if (args[index].equalsIgnoreCase("-nooutput"))
  159.                 _nooutput = true;
  160.             else if (args[index].equalsIgnoreCase("-sun"))
  161.                 _dooaklandsw = false;
  162.             else if (args[index].equalsIgnoreCase("-loop"))
  163.                 _loopCount = Integer.parseInt(args[++index]);
  164.             else if (args[index].equalsIgnoreCase("-proxy"))
  165.                 extractProxy(args[++index]);
  166.             else if (args[index].equalsIgnoreCase("-conproxy"))
  167.                 _useConnectionProxy = true;
  168.             index++;
  169.         }
  170.  
  171.  
  172.         // Crypto algorithms - needed for NTLM, if you want to use
  173.         // a different one then comment out this line and setup the
  174.         // one you like
  175.         Security.addProvider
  176.             (new org.bouncycastle.jce.provider.BouncyCastleProvider());
  177.  
  178.         // SSL - Uncomment this if you are < JDK 1.4
  179.         //Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  180.  
  181.         // Tell Java to use the oaklandsw implementation
  182.         if (_dooaklandsw)
  183.         {
  184.             System.setProperty("java.protocol.handler.pkgs", "com.oaklandsw");
  185.         }
  186.         else
  187.         {
  188.             System.out.println("Using sun implementation");
  189.         }
  190.  
  191.         if (_proxyHost != null && !_useConnectionProxy)
  192.         {
  193.         System.setProperty("http.proxyPort", Integer.toString(_proxyPort));
  194.         System.setProperty("http.proxyHost", _proxyHost);
  195.         System.setProperty("https.proxyPort", Integer.toString(_proxyPort));
  196.         System.setProperty("https.proxyHost", _proxyHost);
  197.         }
  198.         
  199.         // Tells the oaklandsw implementation the object that will
  200.         // resolve the credentials when requested by IIS/NTLM
  201.         com.oaklandsw.http.HttpURLConnection.
  202.             setDefaultUserAgent(userAgent);
  203.  
  204.         String urlStr;
  205.         if (args.length == 0)
  206.             urlStr = "http://www.oaklandsoftware.com";
  207.         else
  208.             urlStr = args[0];
  209.  
  210.         URL url = new URL(urlStr);
  211.         while (true)
  212.         {
  213.             // Wait for a return to be typed
  214.             if (_interactive)
  215.             {
  216.                 System.out.print("Please press enter to connect: ");
  217.                 System.in.read();
  218.                 System.in.read();
  219.             }
  220.  
  221.             try
  222.             {
  223.                 HttpURLConnection urlCon;
  224.                 if (_dooaklandsw)
  225.                 {
  226.                     urlCon = 
  227.                         com.oaklandsw.http.HttpURLConnection.openConnection(url);
  228.                 }
  229.                 else
  230.                 {
  231.                     urlCon = (HttpURLConnection)url.openConnection();
  232.                 }
  233.  
  234.                 if (_proxyHost != null && _useConnectionProxy)
  235.                 {
  236.                     ((com.oaklandsw.http.HttpURLConnection)urlCon).setConnectionProxyHost(_proxyHost);            
  237.                     ((com.oaklandsw.http.HttpURLConnection)urlCon).setConnectionProxyPort(_proxyPort);
  238.                 }
  239.  
  240.                 urlCon.setRequestMethod("GET");
  241.                 urlCon.connect();
  242.  
  243.                 //System.out.println("User-Agent: " + urlCon.getRequestProperty("User-Agent"));
  244.                 if (!_nooutput)
  245.                     System.out.println("Response: " + urlCon.getResponseCode());
  246.  
  247.                 // Print the output stream
  248.                 InputStream inputStream = urlCon.getInputStream();
  249.                 byte[] buffer = new byte[10000];
  250.                 int nb = 0;
  251.                 while (true)
  252.                 {
  253.                     nb = inputStream.read(buffer);
  254.                     if (nb == -1)
  255.                         break;
  256.                     if (!_nooutput)
  257.                         System.out.write(buffer, 0, nb);
  258.                 }
  259.  
  260.             }
  261.             catch (Exception ex)
  262.             {
  263.                 ex.printStackTrace();
  264.             }
  265.  
  266.  
  267.             if (!_interactive)
  268.             {
  269.                 if (--_loopCount == 0)
  270.                     break;
  271.             }
  272.         }
  273.  
  274.     }
  275.  
  276. }
  277.  
  278.  
  279.