---
title: "java 程序代理设置"
author: "liulang203"
date: "2011-03-21T10:33:31Z"
tags: [".net", "HTML", "Java", "Oracle"]
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2012-12-12T14:36:35Z"
id: 969006
url: "https://liulang203.iteye.com/blog/969006"
---

```java
		URL url;
		System.setProperty("http.proxyHost", "192.168.1.1");
		System.setProperty("http.proxyPort", "3128");
		Authenticator.setDefault(new ProxyAuthenticator("name", "password"));

		try {
			url = new URL("http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html");
			InputStream in = url.openStream();
			int i  = -1;
			while((i=in.read())>-1){
				System.out.print((char)i);
			}
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
```


**ProxyAuthenticator.java**

```java


import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class ProxyAuthenticator extends Authenticator {
    private String userName, password; 
    protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(userName, password.toCharArray()); 
    } 
    public ProxyAuthenticator(String userName, String password) { 
        this.userName = userName; 
        this.password = password; 
    } 
}

```

参考资料：<http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html>