资源简介
实验的目的:
1) 掌握Java Socket编程应用;
2) 阅读HTTP协议相关规范;
3) 基于Java Socket构建简单的HTTP的客户端和服务器;
4) 构建支持并发的HTTP服务器。
代码片段和文件信息
package http;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
/**
* Class Client is a class representing a simple HTTP client.
*
* @author wben
*/
public class Client {
/**
* default HTTP port is port 80
*/
private static int port = 80;
/**
* Allow a maximum buffer size of 8192 bytes
*/
private static int buffer_size = 8192;
/**
* The end of line character sequence.
*/
private static String CRLF = “\r\n“;
/**
* Input is taken from the keyboard
*/
static BufferedReader keyboard = new BufferedReader(new InputStreamReader(
System.in));
/**
* Output is written to the screen (standard out)
*/
static PrintWriter screen = new PrintWriter(System.out true);
public static void main(String[] args) throws Exception {
try {
/**
* Create a new HttpClient object.
*/
HttpClient myClient = new HttpClient();
/**
* Parse the input arguments.
*/
if (args.length != 1) {
System.err.println(“Usage: Client “);
System.exit(0);
}
/**
* Connect to the input server
*/
myClient.connect(args[0]);
/**
* Read the get request from the terminal.
*/
screen.println(args[0] + “ is listening to your request:“);
String request = keyboard.readLine();
if (request.startsWith(“GET“)) {
/**
* Ask the client to process the GET request.
*/
myClient.processGetRequest(request);
} else if (request.startsWith(“PUT“)) {
/**
* Ask the client to process the PUT request.
*/
myClient.processPutRequest(request);
} else {
/**
* Do not process other request.
*/
screen.println(“Bad request! \n“);
myClient.close();
return;
}
/**
* Get the headers and display them.
*/
screen.println(“Header: \n“);
screen.print(myClient.getHeader() + “\n“);
screen.flush();
if (request.startsWith(“GET“)) {
/**
* Ask the user to input a name to save the GET resultant web page.
*/
screen.println();
screen.print(“Enter the name of the file to save: “);
screen.flush();
String filename = keyboard.readLine();
FileOutputStream outfile = new FileOutputStream(filename);
/**
* Save the response to the specified file.
*/
String response = myClient.getResponse();
outfile.write(response.getBytes(“iso-8859-1“));
outfile.flush();
outfile.close();
}
/**
* Close the connection client.
*/
myClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 301 2018-12-03 18:59 ex2\.classpath
文件 379 2018-12-03 18:59 ex2\.project
文件 598 2018-12-03 18:59 ex2\.settings\org.eclipse.jdt.core.prefs
文件 27 2018-12-27 21:41 ex2\1.txt
文件 108 2018-12-28 11:30 ex2\2.txt
文件 2654 2018-12-27 20:23 ex2\bin\http\Client.class
文件 6593 2018-12-28 18:43 ex2\bin\http\Handler.class
文件 3859 2018-12-28 12:35 ex2\bin\http\HttpClient.class
文件 1656 2018-12-28 16:39 ex2\bin\http\HttpServer.class
文件 0 2018-12-20 23:17 ex2\cfh
文件 0 2018-12-21 00:05 ex2\PUT
文件 2782 2018-12-03 19:00 ex2\src\http\Client.java
文件 6875 2018-12-28 22:31 ex2\src\http\Handler.java
文件 4777 2018-12-28 12:35 ex2\src\http\HttpClient.java
文件 1053 2018-12-28 22:30 ex2\src\http\HttpServer.java
目录 0 2018-12-28 09:50 ex2\bin\http
目录 0 2018-12-28 09:50 ex2\src\http
目录 0 2018-12-28 09:50 ex2\.settings
目录 0 2018-12-28 09:50 ex2\bin
目录 0 2018-12-28 09:50 ex2\src
目录 0 2018-12-28 22:28 ex2
----------- --------- ---------- ----- ----
31662 21
评论
共有 条评论