-
大小: 6KB文件类型: .zip金币: 1下载: 0 次发布日期: 2021-06-19
- 语言: 其他
- 标签: NWPU Happy_Coder_
资源简介
实现HTTP1.0中的GET和PUT请求响应。内含代码,上传以待大家共同学习。
2019年秋季由王犇老师授教的课程。
由NWPU_Happy_Coder于2019/12/11 15:04上传.
2019年秋季由王犇老师授教的课程。
由NWPU_Happy_Coder于2019/12/11 15:04上传.
代码片段和文件信息
package exec2;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* Class Client is a class representing a simple HTTP client.
*
* @author wben + wjl
*/
public class Client {
private static String getPath = “C:\\Users\\14551\\Desktop\\internet\\getc\\“;//客户端GET到的文件存放路径
/**
* 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
*/
args[0] = “127.0.0.1“;//连接地址
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.
*/
myClient.processPutRequest(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();
int staus = 0;
String str[] = myClient.getHeader().split(“[ \r\n]“);
staus = Integer.valueOf(str[1]);// 获取状态信息
if (request.startsWith(“GET“) && staus == 200) {
/**
* 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(getPath + 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 conn
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3197 2019-12-11 14:45 exec2_http_wjl\Client.java
文件 3959 2019-12-11 14:52 exec2_http_wjl\HttpClient.java
文件 6302 2019-12-11 14:50 exec2_http_wjl\HttpHandler.java
文件 1255 2019-12-11 14:39 exec2_http_wjl\HttpServer.java
目录 0 2019-12-11 14:59 exec2_http_wjl\
评论
共有 条评论