URL Connection Test : Server « Network Protocol « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Network Protocol » ServerScreenshots 
URL Connection Test
URL Connection Test
 
/**
 * version 1.00 1999-08-27 author Cay Horstmann
 */

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;

public class URLConnectionTest {
  public static void main(String[] args) {
    try {
      String urlName;
      if (args.length > 0)
        urlName = args[0];
      else
        urlName = "http://java.sun.com";

      URL url = new URL(urlName);
      URLConnection connection = url.openConnection();

      // set username, password if specified on command line

      if (args.length > 2) {
        String username = args[1];
        String password = args[2];
        String input = username + ":" + password;
        String encoding = base64Encode(input);
        connection.setRequestProperty("Authorization""Basic "
            + encoding);
      }

      connection.connect();

      // print header fields

      int n = 1;
      String key;
      while ((key = connection.getHeaderFieldKey(n)) != null) {
        String value = connection.getHeaderField(n);
        System.out.println(key + ": " + value);
        n++;
      }

      // print convenience functions

      System.out.println("----------");
      System.out
          .println("getContentType: " + connection.getContentType());
      System.out.println("getContentLength: "
          + connection.getContentLength());
      System.out.println("getContentEncoding: "
          + connection.getContentEncoding());
      System.out.println("getDate: " + connection.getDate());
      System.out.println("getExpiration: " + connection.getExpiration());
      System.out.println("getLastModifed: "
          + connection.getLastModified());
      System.out.println("----------");

      BufferedReader in = new BufferedReader(new InputStreamReader(
          connection.getInputStream()));

      // print first ten lines of contents

      String line;
      n = 1;
      while ((line = in.readLine()) != null && n <= 10) {
        System.out.println(line);
        n++;
      }
      if (line != null)
        System.out.println(". . .");
    catch (IOException exception) {
      System.out.println("Error: " + exception);
    }
  }

  public static String base64Encode(String s) {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    Base64OutputStream out = new Base64OutputStream(bOut);
    try {
      out.write(s.getBytes());
      out.flush();
    catch (IOException exception) {
    }
    return bOut.toString();
  }
}

/*
 * BASE64 encoding encodes 3 bytes into 4 characters.
 * |11111122|22223333|33444444| Each set of 6 bits is encoded according to the
 * toBase64 map. If the number of input bytes is not a multiple of 3, then the
 * last group of 4 characters is padded with one or two = signs. Each output
 * line is at most 76 characters.
 */

class Base64OutputStream extends FilterOutputStream {
  public Base64OutputStream(OutputStream out) {
    super(out);
  }

  public void write(int cthrows IOException {
    inbuf[i= c;
    i++;
    if (i == 3) {
      super.write(toBase64[(inbuf[00xFC>> 2]);
      super.write(toBase64[((inbuf[00x03<< 4)
          ((inbuf[10xF0>> 4)]);
      super.write(toBase64[((inbuf[10x0F<< 2)
          ((inbuf[20xC0>> 6)]);
      super.write(toBase64[inbuf[20x3F]);
      col += 4;
      i = 0;
      if (col >= 76) {
        super.write('\n');
        col = 0;
      }
    }
  }

  public void flush() throws IOException {
    if (i == 1) {
      super.write(toBase64[(inbuf[00xFC>> 2]);
      super.write(toBase64[(inbuf[00x03<< 4]);
      super.write('=');
      super.write('=');
    else if (i == 2) {
      super.write(toBase64[(inbuf[00xFC>> 2]);
      super.write(toBase64[((inbuf[00x03<< 4)
          ((inbuf[10xF0>> 4)]);
      super.write(toBase64[(inbuf[10x0F<< 2]);
      super.write('=');
    }
  }

  private static char[] toBase64 = 'A''B''C''D''E''F''G''H',
      'I''J''K''L''M''N''O''P''Q''R''S''T''U',
      'V''W''X''Y''Z''a''b''c''d''e''f''g''h',
      'i''j''k''l''m''n''o''p''q''r''s''t''u',
      'v''w''x''y''z''0''1''2''3''4''5''6''7',
      '8''9''+''/' };

  private int col = 0;

  private int i = 0;

  private int[] inbuf = new int[3];
}


           
         
  
Related examples in the same category
1. A generic framework for a flexible, multi-threaded server
2. Server allows connections on socket 6123Server allows connections on socket 6123
3. This server displays messages to a single clientThis server displays messages to a single client
4. The client can specify information to control the output of a serverThe client can specify information to control the output of a server
5. A server can use specialized streams to deliver typed dataA server can use specialized streams to deliver typed data
6. Serve entire objects using ObjectOutputStreamServe entire objects using ObjectOutputStream
7. A multithreaded serverA multithreaded server
8. Base class to build multithreaded servers easily
9. Manage a pool of threads for clients
10. Client estimates the speed of the network connection to the server
11. Socket Openeration Test
12. This server retrieves the time using the RFC867 protocol.This server retrieves the time using the RFC867 protocol.
13. Quote Server
14. Logging Server based on SocketServer
15. Client and Server Demo
16. Reflector
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.