Web service 基本认证相关源码
2013-02-05 10:18
330 查看
转自:http://www.rgagnon.com/javadetails/java-0085.html。
One way is to use the HTTP property "Proxy-Authorization" with a username:password base64 encoded.
NOTE: For a base64 function, see this
How-to.
The following example dumps the content of a URL but before we identify ourself to the proxy.
With JDK1.2, the
java.net.Authenticator can be used to send the credentials when needed.
The http.nonProxyHosts property indicates the hosts which should be connected too directly and not through the proxy server. The value can be a list of hosts, each seperated by a |, and in addition a wildcard character (*) can be used for matching.
Proxy and Username/Password
You might need to identify yourself to the proxy server.One way is to use the HTTP property "Proxy-Authorization" with a username:password base64 encoded.
System.setProperty("http.proxyHost", "myProxyServer.com"); System.setProperty("http.proxyPort", "80"); URL url=new URL("http://someserver/somepage"); URLConnection uc = url.openConnection (); String encoded = new String (Base64.base64Encode(new String("username:password").getBytes())); uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded); uc.connect();
NOTE: For a base64 function, see this
How-to.
The following example dumps the content of a URL but before we identify ourself to the proxy.
import java.net.*; import java.io.*; public class URLUtils { public static void main(String s[]) { URLUtils.dump("http://www.yahoo.com"); System.out.println("**************"); URLUtils.dump("https://www.paypal.com"); System.out.println("**************"); } public static void dump(String URLName){ try { DataInputStream di = null; FileOutputStream fo = null; byte [] b = new byte[1]; // PROXY System.setProperty("http.proxyHost","proxy.mydomain.local") ; System.setProperty("http.proxyPort", "80") ; URL u = new URL(URLName); HttpURLConnection con = (HttpURLConnection) u.openConnection(); // // it's not the greatest idea to use a sun.misc.* class // Sun strongly advises not to use them since they can // change or go away in a future release so beware. // sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder(); String encodedUserPwd = encoder.encode("mydomain\\MYUSER:MYPASSWORD".getBytes()); con.setRequestProperty ("Proxy-Authorization", "Basic " + encodedUserPwd); // PROXY ---------- di = new DataInputStream(con.getInputStream()); while(-1 != di.read(b,0,1)) { System.out.print(new String(b)); } } catch (Exception e) { e.printStackTrace(); } } }
With JDK1.2, the
java.net.Authenticator can be used to send the credentials when needed.
public static void dump(String URLName){ try { DataInputStream di = null; FileOutputStream fo = null; byte [] b = new byte[1]; // PROXY System.setProperty("http.proxyHost","proxy.mydomain.local") ; System.setProperty("http.proxyPort", "80") ; Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("mydomain\\username","password".toCharArray()); }}); URL u = new URL(URLName); HttpURLConnection con = (HttpURLConnection) u.openConnection(); di = new DataInputStream(con.getInputStream()); while(-1 != di.read(b,0,1)) { System.out.print(new String(b)); } } catch (Exception e) { e.printStackTrace(); } }
Bypass a Proxy
In intranet environment, you may need to bypass the proxy server and go directly to the http server.The http.nonProxyHosts property indicates the hosts which should be connected too directly and not through the proxy server. The value can be a list of hosts, each seperated by a |, and in addition a wildcard character (*) can be used for matching.
java.exe
相关文章推荐
- JDK源码解析基础篇-8种基本数据类型的包装类及相关讨论
- CppUnit源码解读(2)--基本测试相关类
- uc/os-II 源码剖析笔记(1)——几个OS相关的基本概念(二)
- 4 Handler相关类——Live555源码阅读(一)基本组件类
- 12 哈希表相关类——Live555源码阅读(一)基本组件类
- 第二章----1,身份认证的基本流程,附上源码
- 8 延时队列相关类——Live555源码阅读(一)基本组件类
- springmvc基本配置及相关源码解读
- 19 BasicTaskScheduler0 基本任务调度类基类(一)——Live555源码阅读(一)任务调度相关类
- 20 BasicTaskScheduler0 基本任务调度类基类(二)——Live555源码阅读(一)任务调度相关类
- MaNGOS-Zero源码学习之realmd认证登录服务器(一):认证登录基本流程
- 21 BasicTaskScheduler基本任务调度器(一)——Live555源码阅读(一)任务调度相关类
- 构建RESTful Web Service - 验证的实现和使用(HTTP 基本认证)
- Hadoop RPC 源码分析- 相关类图
- 二分搜索的基本相关问题
- 非典型2D游戏引擎 Orx 源码阅读笔记(4) 用C实现的基本容器(List,HashTable,Tree)
- [python相关]BaseHTTPServer -- 基本的 HTTP 服务器
- LDA基本介绍以及LDA源码分析(BLEI)
- mysql数据库相关基本术语和概念
- 块状链表基本操作及相关例题