Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1501582
  • 博文数量: 3500
  • 博客积分: 6000
  • 博客等级: 准将
  • 技术积分: 43870
  • 用 户 组: 普通用户
  • 注册时间: 2008-05-03 20:31
文章分类

全部博文(3500)

文章存档

2008年(3500)

我的朋友

分类:

2008-05-04 19:23:01

一起学习
利用Authenticator技术访问外部http server。 import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; public class URLPassword extends Frame { private TextField tf = new TextField(); private TextArea ta = new TextArea(); public URLPassword() { super ("URL Password"); // 安装 Authenticator Authenticator.setDefault (new MyAuthenticator ()); // 设置屏幕 add (tf, BorderLayout.NORTH); ta.setEditable(false); add (ta, BorderLayout.CENTER); tf.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { String s = tf.getText(); if (s.length() != 0) ta.setText (fetchURL (s)); } }); addWindowListener (new WindowAdapter() { public void windowClosing (WindowEvent e) { dispose(); System.exit(0); } }); } private String fetchURL (String urlString) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); try { URL url = new URL (urlString); InputStream content = (InputStream)url.getContent(); BufferedReader in = new BufferedReader (new InputStreamReader (content)); String line; while ((line = in.readLine()) != null) { pw.println (line); } } catch (MalformedURLException e) { pw.println ("Invalid URL"); } catch (IOException e) { pw.println ("Error reading URL"); } return sw.toString(); } public static void main (String args[]) { Frame f = new URLPassword(); f.setSize(300, 300); f.setVisible (true); } class MyAuthenticator extends Authenticator { protected PasswordAuthentication getPasswordAuthentication() { final Dialog jd = new Dialog (URLPassword.this, "Enter password", true); jd.setLayout (new GridLayout (0, 1)); Label jl = new Label (getRequestingPrompt()); jd.add (jl); TextField username = new TextField(); username.setBackground (Color.lightGray); jd.add (username); TextField password = new TextField(); password.setEchoChar ('*'); password.setBackground (Color.lightGray); jd.add (password); Button jb = new Button ("OK"); jd.add (jb); jb.addActionListener (new ActionListener() { public void actionPerformed (ActionEvent e) { jd.dispose(); } }); jd.pack(); jd.setVisible(true); String pass= new String(password.getText()); return new PasswordAuthentication (username.getText(),pass.toCharArray()); } } } 下载本文示例代码


利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server利用Authenticator技术访问proxy外部http server
阅读(196) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~