DOC

3189.docjava

By Sue Baker,2014-10-15 22:55
9 views 0
3189.docjava

    由于程序的原因;本文件未被完整保存。

    首页 ? 研究文集 ? I/O及网络

    Http server学习版以及通过代理服务器认证

    vedadou 整理 (参与分~38310;专家分~545) 发表~2004-05-10 11:08 版本~1.0 阅读~2247

    一、Http Server的最简单版本

    这个版本不能在简单了;有助于学习

    1.

    2. import java.io.*;

    3. import java.net.*;

    4. import java.util.*;

    5.

    6. public class httpd extends Thread {

    7. public static final int HTTP_PORT = 8999; 8. protected ServerSocket listen;

    9.

    10. public static void main (String args[]) throws IOException {

    11. System.out.println("Starting Server..."); 12. new httpd();

    13. System.out.println("Http Server Started,OK!"); 14. }

    15. public httpd () {

    16. try {

    17. listen = new ServerSocket(HTTP_PORT); 18. } catch (IOException e) {

    19. System.out.println("Error in creating server socket");

    20. }

    21. this.start();

    22. }

    23. public void run () {

    24. try {

    25. while (true) {

    26. Socket client = listen.accept(); 27. Connect cc = new Connect(client); 28. }

    29. } catch (IOException e) {

    30. }

    31.

    32. }

    33.

    34.}

35.

    36./**

    37. * socket连接

    38. */

    39.class Connect extends Thread {

    40. Socket client;

    41. DataInputStream is;

    42. public OutputStream rawOut = null;

    43. public DataOutputStream out = null;

    44.

    45. public Connect (Socket s) {

    46. client = s;

    47. try {

    48. is = new DataInputStream(client.getInputStream()); 49. rawOut = client.getOutputStream(); 50. out = new DataOutputStream(rawOut); 51. } catch (IOException e) {

    52. try {

    53. client.close();

    54. } catch (IOException ex) {

    55. }

    56. }

    57. this.start();

    58. }

    59.

    60. public void run () {

    61. try {

    62. String request = is.readUTF(); 63. System.out.println("请求~" + request); 64. StringTokenizer st = new StringTokenizer(request); 65. if ((st.countTokens() >= 2) && st.nextToken().equals("GET")) {

    66. if ((request = st.nextToken()).startsWith("/")) 67. request = request.substring(1).trim(); 68. if (request.endsWith("/") || request.equals("")) 69. request = request + "index.html"; 70. File f = new File(request.trim()); 71. shipDocument(out, f);

    72. } else {

    73. out.writeBytes("400 Bad Request"); 74. }

    75. client.close();

    76. System.out.println("关闭~" + client.getPort()); 77. } catch (IOException ex1) {

    78. }

79. }

    80.

    81. synchronized public static void shipDocument (DataOutputStream out, File f) {

    82. DataInputStream in = null;

    83. try {

    84.

    85. in = new DataInputStream(new FileInputStream(f)); 86. int len = (int) f.length();

    87. byte buf[] = new byte[8192];

    88. System.out.println("web文件名:" + f.getName() + " length:" + f.length());

    89. FileInputStream file = new FileInputStream(f); 90.

    91.

    92. out.writeBytes("HTTP/1.1 200 OK \r\n"); 93. out.writeBytes("Content-Length:" + buf.length + " \r\n");

    94. out.writeBytes("Content-Type:text\\html\r\n\r\n"); 95.

    96. for (int i = 0; i < len; i += 8192) { 97. int bytesRead = file.read(buf); 98. out.write(buf, 0, bytesRead); 99.

    100. }

    101.

    102. out.writeBytes("\r\n");

    103. out.flush();

    104. in.close();

    105. file.close();

    106. } catch (FileNotFoundException ex) { 107. try {

    108. out.writeBytes("404 not found!"); 109. } catch (IOException e) { 110.

    111. }

    112. } catch (IOException e) {

    113. System.out.println("err"); 114. }

    115.

    116. }

    117.

    118. }

    二、 代理服务器认证的实现

     扩展JR开源项目的网络蚂蚁;通过代理服务器;弹出认证窗口。 1.

    2. import java.io.*;

    3. import java.net.*;

    4. import java.awt.*;

    5. import java.awt.event.*;

    6. import java.util.*;

    7.

    8. public class URLPassword extends Frame { 9. private TextField tf = new TextField(); 10. private TextArea ta = new TextArea(); 11.

    12. public URLPassword () {

    13.

    14. super("URL Password");

    15.// 安装 Authenticator

    16. MyAuthenticator myAuth = new MyAuthenticator(); 17. Authenticator.setDefault(myAuth); 18.// 设置屏幕

    19. add(tf, BorderLayout.NORTH); 20. ta.setEditable(false);

    21. add(ta, BorderLayout.CENTER); 22. tf.addActionListener(new ActionListener() { 23. public void actionPerformed (ActionEvent e) {

    24. String s = tf.getText(); 25. if (s.length() != 0) 26. ta.setText(fetchURL(s)); 27. }

    28. });

    29. addWindowListener(new WindowAdapter() { 30. public void windowClosing (WindowEvent e) { 31. dispose();

    32. System.exit(0);

    33. }

    34. });

    35. }

    36. private String fetchURL (String urlString) { 37. StringWriter sw = new StringWriter(); 38. PrintWriter pw = new PrintWriter(sw); 39.

    40. try {

    41. URL url = new URL(urlString);

    42. InputStream content = (InputStream) url.getContent(); 43. BufferedReader in =

    44. new BufferedReader(new InputStreamReader(content));

    45. String line;

    46. while ((line = in.readLine()) != null) { 47. pw.println(line);

    48. }

    49. } catch (MalformedURLException e) {

    50. pw.println("Invalid URL");

    51. } catch (IOException e) {

    52. pw.println("Error reading URL"); 53. }

    54. return sw.toString();

    55. }

    56.

    57. public static void main (String args[]) { 58. Properties systemSettings = System.getProperties(); 59. systemSettings.put("proxySet", "true"); 60. systemSettings.put("proxyHost", "202.112.209.106"); 61. systemSettings.put("proxyPort", "8888"); 62. System.setProperties(systemSettings); 63. Frame f = new URLPassword();

    64. f.setSize(300, 300);

    65. f.setVisible(true);

    66. }

    67. class MyAuthenticator extends Authenticator { 68.

    69. protected PasswordAuthentication getPasswordAuthentication () {

    70.

    71. final Dialog jd = new Dialog(URLPassword.this, "Enter password", true);

    72. jd.setLayout(new GridLayout(0, 1)); 73. Label jl = new Label(getRequestingPrompt()); 74. jd.add(jl);

    75. TextField username = new TextField(); 76. username.setBackground(Color.lightGray); 77. jd.add(username);

    78. TextField password = new TextField(); 79. password.setEchoChar('*');

    80. password.setBackground(Color.lightGray); 81. jd.add(password);

    82. Button jb = new Button("OK");

    83. jd.add(jb);

    84. jb.addActionListener(new ActionListener() { 85. public void actionPerformed (ActionEvent e) {

    86. jd.dispose();

    87. }

    88. });

    89. jd.pack();

    90. jd.setVisible(true);

    91. return new PasswordAuthentication(username.getText(), password.getText().toCharArray());

    92. }

    93.

    94. }

    95.}

版权声明 本篇文章对您是否有帮助? 投票~ 投票结果~

    作者其它文章~

    ; TOMCAThttps替换http的方法

    ; CORBA平台 JacORB2.1 学习笔记--入门篇

    ; Web应用中避免Form重复提交的三种方案

    ; Hibernate技巧,1:~利用配置文件编写程序生成数据库

    ; Java类库实践点滴,一: —— LoggingObjectassert 作者全部文章 上一篇文章 下一篇文章 返回〔I/O及网络〕

Report this document

For any questions or suggestions please email
cust-service@docsford.com