Code/Resource
Windows Develop
Linux-Unix program
Internet-Socket-Network
Web Server
Browser Client
Ftp Server
Ftp Client
Browser Plugins
Proxy Server
Email Server
Email Client
WEB Mail
Firewall-Security
Telnet Server
Telnet Client
ICQ-IM-Chat
Search Engine
Sniffer Package capture
Remote Control
xml-soap-webservice
P2P
WEB(ASP,PHP,...)
TCP/IP Stack
SNMP
Grid Computing
SilverLight
DNS
Cluster Service
Network Security
Communication-Mobile
Game Program
Editor
Multimedia program
Graph program
Compiler program
Compress-Decompress algrithms
Crypt_Decrypt algrithms
Mathimatics-Numerical algorithms
MultiLanguage
Disk/Storage
Java Develop
assembly language
Applications
Other systems
Database system
Embeded-SCM Develop
FlashMX/Flex
source in ebook
Delphi VCL
OS Develop
MiddleWare
MPI
MacOS develop
LabView
ELanguage
Software/Tools
E-Books
Artical/Document
GetAuthInfo.java
Package: HTTP客户端.zip [view]
Upload User: demmber
Upload Date: 2007-12-22
Package Size: 717k
Code Size: 3k
Category:
Java Develop
Development Platform:
Java
- import HTTPClient.HTTPConnection;
- import HTTPClient.HTTPResponse;
- import HTTPClient.URI;
- import HTTPClient.NVPair;
- import HTTPClient.AuthorizationInfo;
- import HTTPClient.DefaultAuthHandler;
- import HTTPClient.AuthorizationPrompter;
- public class GetAuthInfo
- {
- public static void main(String args[]) throws Exception
- {
- String pa_name = null, pa_pass = null;
- if (args.length == 4 && "-proxy_auth".startsWith(args[0]))
- {
- pa_name = args[1];
- pa_pass = args[2];
- String[] tmp = { args[3] };
- args = tmp;
- }
- if (args.length != 1 || args[0].equalsIgnoreCase("-help"))
- {
- System.err.println("Usage: java GetAuthInfo [-proxy_auth <username> <password>] <url>");
- System.exit(1);
- }
- URI url = new URI(args[0]);
- DefaultAuthHandler.setAuthorizationPrompter(new MyAuthPrompter(pa_name, pa_pass));
- HTTPConnection con = new HTTPConnection(url);
- HTTPResponse rsp = con.Head(url.getPathAndQuery());
- int sts = rsp.getStatusCode();
- if (sts < 300)
- System.out.println("No authorization required to access " + url);
- else if (sts >= 400 && sts != 401 && sts != 407)
- System.out.println("Error trying to access " + url + ":n" + rsp);
- }
- }
- class MyAuthPrompter implements AuthorizationPrompter
- {
- private String pa_name, pa_pass;
- private boolean been_here = false;
- MyAuthPrompter(String pa_name, String pa_pass)
- {
- this.pa_name = pa_name;
- this.pa_pass = pa_pass;
- }
- public NVPair getUsernamePassword(AuthorizationInfo challenge, boolean forProxy) {
- if (forProxy && pa_name != null)
- {
- if (been_here)
- {
- System.out.println();
- System.out.println("Proxy authorization failed");
- return null;
- }
- been_here = true;
- return new NVPair(pa_name, pa_pass);
- }
- if (been_here)
- {
- System.out.println();
- System.out.println("Proxy authorization succeeded");
- }
- // print out all challenge info
- System.out.println();
- if (forProxy)
- System.out.println("The proxy requires authorization");
- else
- System.out.println("The server requires authorization for this resource");
- System.out.println();
- System.out.println("Scheme: " + challenge.getScheme());
- System.out.println("Realm: " + challenge.getRealm());
- System.out.println();
- System.out.println("Add the following line near the beginning of your application:");
- System.out.println();
- if (challenge.getScheme().equalsIgnoreCase("Basic"))
- System.out.println(" AuthorizationInfo.addBasicAuthorization(""+
- challenge.getHost() + "", " +
- challenge.getPort() + ", "" +
- challenge.getRealm() + "", " +
- "<username>, <password>);");
- else if (challenge.getScheme().equalsIgnoreCase("Digest"))
- System.out.println(" AuthorizationInfo.addDigestAuthorization("" +
- challenge.getHost() + "", " +
- challenge.getPort() + ", "" +
- challenge.getRealm() + "", " +
- "<username>, <password>);");
- else
- System.out.println(" AuthorizationInfo.addAuthorization("" +
- challenge.getHost() + "", " +
- challenge.getPort() + ", "" +
- challenge.getScheme() + "", "" +
- challenge.getRealm() + "", " +
- "...);");
- System.out.println();
- return null;
- }
- }