Web service 基本认证相关源码

本文介绍了如何在Java中配置HTTP代理并使用基本认证。通过设置系统属性和使用`Proxy-Authorization`请求头,可以实现通过带用户名和密码的代理服务器访问网页。此外,还提供了绕过特定主机代理的方法。
摘要由CSDN通过智能技术生成

转自:http://www.rgagnon.com/javadetails/java-0085.html

 

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  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值