频 道 直 达 - 新闻 - 读书 - 培训 - 教程 - 前沿 - 组网 - 系统应用 - 安全 - 编程 - 存储 - 操作系统 - 数据库 - 服务器 - 专题 - 产品 - 案例库 - 技术圈 - 博客 - BBS
51CTO.COM_中国领先的IT技术网站
找资料:

用J2SE 1.4进行Internet安全编程(上)(3)

作者: Qusay H. Mahmoud 著 边城狂人 译 出处:ccw  (  ) 砖  (  ) 好  评论 ( ) 条  进入论坛
更新时间:2006-03-22 10:47
关 键 词:J2SE
阅读提示:这篇文章介绍了 SSL 并描述了 JSSE 框架及其实现,详细说明了 SSL 和 JSSE,并且说明了如何开发服务器端支持 SSL 应用程序。
不安全的 HTTP 服务器

为了开发一个 HTTP 服务器,你得先搞明白 HTTP 协议是如何工作的。这个服务器是一个只支持 GET 请求方法的简单服务器。代码示例 1 是这个例子的实现。这是一个多线程的 HTTP 服务器,ProcessConnection 类用于执行不同线程中新的请求。当服务器收到一个来自浏览器的请求时,它解析这个请求并找出需要的文档。如果被请求的文档在服务器上可用,那么被请求的文档会由 shipDocument 方法送到服务器。如果被请求的文档没有打开,那么送到服务器的就是出错消息。

代码示例 1:HttpServer.java

import java.io.*;

import java.net.*;

import java.util.StringTokenizer;

/**

* This class implements a multithreaded simple HTTP

* server that supports the GET request method.

* It listens on port 44, waits client requests, and

* serves documents.

*/

public class HttpServer {

// The port number which the server

// will be listening on

public static final int HTTP_PORT = 8080;

public ServerSocket getServer() throws Exception {

return new ServerSocket(HTTP_PORT);

}

// multi-threading -- create a new connection

// for each request

public void run() {

ServerSocket listen;

try {

listen = getServer();

while(true) {

Socket client = listen.accept();

ProcessConnection cc = new

ProcessConnection(client);

}

} catch(Exception e) {

System.out.println("Exception:

"+e.getMessage());

}

}

// main program

public static void main(String argv[]) throws

Exception {

HttpServer httpserver = new HttpServer();

httpserver.run();

}

}

class ProcessConnection extends Thread {

Socket client;

BufferedReader is;

DataOutputStream os;

public ProcessConnection(Socket s) { // constructor client = s;

try {

is = new BufferedReader(new InputStreamReader

(client.getInputStream()));

os = new DataOutputStream(client.getOutputStream());

} catch (IOException e) {

System.out.println("Exception: "+e.getMessage());

}this.start(); // Thread starts here...this start()

will call run()

}

public void run() {

try {

// get a request and parse it.

String request = is.readLine();

System.out.println( "Request: "+request );

StringTokenizer st = new StringTokenizer( request );

if ( (st.countTokens() >= 2) &&

st.nextToken().equals("GET") ) {

if ( (request =

st.nextToken()).startsWith("/") )

request = request.substring( 1 );

if ( request.equals("") )

request = request + "index.html";

File f = new File(request);

shipDocument(os, f);

} else {

os.writeBytes( "400 Bad Request" );

}

client.close();

} catch (Exception e) {

System.out.println("Exception: " +

e.getMessage());

}

}

/**

* Read the requested file and ships it

* to the browser if found.

*/

public static void shipDocument(DataOutputStream out,

File f) throws Exception {

try {

DataInputStream in = new

DataInputStream(new FileInputStream(f));

int len = (int) f.length();

byte[] buf = new byte[len];

in.readFully(buf);

in.close();

out.writeBytes("HTTP/1.0 200 OK\r\n");

out.writeBytes("Content-Length: " + f.length() +"\r\n");

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

out.write(buf);

out.flush();

}

catch (Exception e) {out.writeBytes("\r\n\r\n");

out.writeBytes("HTTP/1.0 400 " + e.getMessage() + "\r\n");

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

out.writeBytes("");

out.flush();

} finally {

out.close();

}

}

}


共6页: 上一页 [1] [2] 3 [4] [5] [6] 下一页
【内容导航】
发表
查看
我也说两句

匿名发表

(如果看不清请点击图片进行更换)


中 国 领 先 的 IT 技 术 网 站 ·
技 术 成 就 梦 想
·Java基础教程 (查看52078次)
·UML类图详解 (查看46604次)
·Java编程开发手册 (查看24992次)
·UML统一建模语言 (查看23997次)
·C#技术开发指南 (查看22188次)
·Java编程开发手册 (1195个砖)
·Java基础教程 (429个砖)
·C#技术开发指南 (304个砖)
·PB开发教程 (220个砖)
·.NET开发手册 (217个砖)
·Java编程开发手册 (653个好)
·Java基础教程 (569个好)
·.NET开发手册 (249个好)
·PB开发教程 (209个好)
·Delphi开发技术手册 (174个好)
订阅技术快讯
电子杂志下载
名称:网络安全精品应用黄皮书
简介:《2007精品网络安全黄皮书》包括了9个大类24个小类, 800余篇文章,内容包含了熊猫烧香病毒、DDOS攻击、ARP病等热点问题的介绍及解决方案。从病毒查杀、防范、系统、数据等各方面的安全设置到黑客技术的了解、防范,涉及到了安全应用的全部领域, 由浅至深内容全面。
名称:Vista精品应用黄皮书
简介:《Vista精品应用黄皮书》囊括了Vista的各方面内容。此次的精简版,是将里面的内容做了提取,便于用户下载和使用。内容包含了各种Vista的安装与实施、技巧与解析以及各种Vista相关学习文档和相关软件的安全下载。该电子书是了解和应用Vista人员必备的工具手册,并且也是第一本
名称:2006中国IT论坛精品集合
简介:本书由“51CTO论坛推广联盟”制作完成。书中所有内容均来自各联盟成员的论坛(网站)。制作本书的目的是为了集中大家的优势资源,将更多更精彩的内容带给广大技术爱好者。本书是联盟成立以来制作的第一本书。
关键字阅读
频道精选
主编信箱 热线:010-66476606 告诉我们您想看的:专题 文章
关于我们 | 诚聘英才 | 联系我们 | 网站大事 | 意见反馈 | 网站地图
Copyright©2005-2007 51CTO.COM 版权所有