在前几节我们详细讲解了http协议的相关信息,基于“知行合一”的原则,只有通过具体动手实践才有可能检验知识点被我们真正掌握,本节我们就使用代码实现http的get请求。
首先需要一个http服务器,基于简单原则,我使用了在手机上的福昕pdf阅读器iPhone版,安卓版效用一样,它自身附带了小型http服务器,用于将文档从电脑传给手机,打开该App,在菜单中选择Wi-Fi,点击“允许文件传输”即可启动http服务器,然后在浏览器中输入“http://192.168.2.127:8888″,其中192.168.2.127是我手机的ip,也对应http服务器的ip,8888是服务器接收请求的端口,输入URL点击后,浏览器会接收到请求回应并展现如下:
使用它的好处在于足够简单,并且它有文件上传功能,于是后面我们还可以用来实现POST请求,接下来我们使用代码模拟客户端向它发送GET请求,首先实现的是http数据包组装和解析功能:
package Application;
import java.util.*;
public class HTTPEncoder {
public enum HTTP_METHOD {
HTTP_GET,
HTTP_HEADER,
HTTP_POST,
HTTP_OPTIONS
};
private String http_method = "";
private String http_header = "";
private Map<String, String> http_header_map = new HashMap<String, String>();
public void set_method(HTTP_METHOD method, String url) {//构造http 请求方法字符串
switch (method) {
case HTTP_GET:
http_method = "GET ";
break;
case HTTP_HEADER:
http_method = "HEADER ";
break;
case HTTP_POST:
http_method = "POST ";
break;
case HTTP_OPTIONS:
http_method = "OPTIONS ";
break;
}
http_method += url;
http_method += " HTTP/1.1\r\n"; //必须以/r/n结尾
}
public void set_header(String header_name, String header_value) {//将头部信息中的字段和对应值对应起来
http_header_map.put(header_name, header_value);
}
public String get_http_content() {//把起始行和头部字段信息组装成http请求数据包文本
String http_content = "";
http_content += http_method;
for (Map.Entry<String, String> entry : http_header_map.entrySet()) {
String header_name = entry.getKey();
String header_value = entry.getValue();
http_header += header_name;
http_header += ": ";
http_header += header_value;
http_header += "\r\n";
}
http_content += http_header;
http_content += "\r\n";
return http_content;
}
public int get_return_code(String http_return) {
String http_code_begin_str = "HTTP/1.1 "; //获取http回复包的返回码,200表示请求被正常处理
int http_code_begin = http_return.indexOf(http_code_begin_str);
if ( http_code_begin!= 0) {
return -1;
}
http_code_begin += http_code_begin_str.length();
String http_code_end_str = " ";
int http_code_end = http_return.indexOf(http_code_end_str, http_code_begin);
if (http_code_end == -1) {
return -1;
}
String http_code = http_return.substring(http_code_begin, http_code_end);
int code = Integer.parseInt(http_code); //通过Content-Length字段获取服务器返回数据长度
return code;
}
public int get_content_length(String http_return) {
String http_content_length = "Content-Length: ";
String http_line_end = "\r\n";
int length_begin = http_return.indexOf(http_content_length);
if (length_begin == -1) {
return 0;
}
length_begin += http_content_length.length();
int length_end = http_return.indexOf(http_line_end, length_begin);
String content_length_str = http_return.substring(length_begin, length_end);
int content_length = Integer.parseInt(content_length_str);
return content_length;
}
}
上面给定的类用于负责组装http请求的方法行,同时将http请求的头部字段和对应信息放入到一个Map中以便对应起来,然后通过get_http_content方法将请求行以及头部字段信息组合成http请求数据包的内容。该类还实现了简单的http返回数据解析,它解读服务器返回的http数据,确保返回码是200,也就是服务器正常处理了我们发出的http请求,同时通过Content-Length字段获取服务器返回的数据长度。
接着我们使用该类发送请求数据包:
package Application;
import java.net.InetAddress;
import utils.ITCPHandler;
public class HTTPClient implements ITCPHandler{
private TCPThreeHandShakes tcp_socket = null;
private HTTPEncoder httpEncoder = new HTTPEncoder();
private String user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 1-14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/547.36";
private String accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3q=0.9";
private int total_length = 0;
private String http_return_content = "";
private boolean receive_first_time = true;
private int receive_content_length = 0;
private static int HTTP_OK = 200;
private void send_content() throws Exception {//设置http请求数据的头部信息
httpEncoder.set_method(HTTPEncoder.HTTP_METHOD.HTTP_GET, "/");
httpEncoder.set_header("Host","192.168.2.127:8888");
httpEncoder.set_header("Connection", "keep-alive");
httpEncoder.set_header("Upgrade-Insecure-Requests", "1");
httpEncoder.set_header("User-Agent", user_agent);
httpEncoder.set_header("Accept", accept);
httpEncoder.set_header("Purpose", "Prefetch");
httpEncoder.set_header("Accept-Encoding", "gzip,deflate");
httpEncoder.set_header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8");
String http_content = httpEncoder.get_http_content();
System.out.println(http_content);
byte[] send_content = http_content.getBytes();
tcp_socket.tcp_send(send_content);
}
@Override
public void connect_notify(boolean connect_res) {
if (connect_res == true) {
System.out.println("connect http server ok!");
try {
send_content();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else {
System.out.println("connect http server fail!");
}
}
@Override
public void send_notify(boolean send_res, byte[] packet_send) {
if (send_res) {
System.out.println("send request to http server!");
}
}
private void close_connection() {
try {
tcp_socket.tcp_close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void recv_notify(byte[] packet_recv) {
String content = new String(packet_recv);
if (receive_first_time) {
/*
如果是第一次接收服务器返回的数据,那么从http返回的头部获取请求处理结果码和返回数据长度等信息,
要不然就是获取服务器返回的数据信息
*/
receive_first_time = false;
int code = httpEncoder.get_return_code(content);
if (code != HTTP_OK) {
System.out.println("http return error: " + code);
close_connection();
return;
} else {
System.out.println("Http return 200 OK!");
}
this.total_length = httpEncoder.get_content_length(content);
if (this.total_length <= 0) {
close_connection();
return;
}
System.out.println("Content Length: " + total_length);
}
else {
//每次接收数据后检查接收数据长度是否达到指定长度,达到表明服务器发送数据完毕,可以关闭连接
http_return_content += content;
receive_content_length += packet_recv.length;
if (receive_content_length >= total_length) {
System.out.println("receive content is: " + http_return_content);
close_connection();
}
}
}
@Override
public void connect_close_notify(boolean close_res) {
if (close_res) {
System.out.println("Close connection with http server!");
}
}
public void run() {
try {
InetAddress ip = InetAddress.getByName("192.168.2.127"); //连接ftp服务器
short port = 8888;
tcp_socket = new TCPThreeHandShakes(ip.getAddress(), port, this);
tcp_socket.tcp_connect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
该类使用HttpEncoder类构造http请求数据包后,调用前面实现的tcp协议将数据包封装起来发送给服务器,一旦收到服务器的返回后解析返回数据内容,获取http返回码,确认返回码为200也就是服务器正常处理发过去的请求,然后获得服务器返回的数据长度。
接下来就在函数recv_notify中依次接收服务器发送过来的数据信息,每次接收信息后计算总共接收到的数据长度是否与服务器返回的Content-Length字段中指定的长度一致,如果一致说明信息全部接收完毕,那么程序将接收到的信息打印出来并关闭tcp连接,上面代码运行后在wireshark中的抓包如下:
从截图中看到,GET / HTTP/1.1就是程序构造的请求包,HTTP/1.1 200 OK就是服务器返回的应答,从回复看服务器接受了程序发出的请求并正常处理,同时将相关信息返回给程序,程序在接收完所有信息后将接收到的数据打印出来,结果如下:
从截图看到,程序正确接收到服务器返回的html页面信息,由此可见我们代码的实现逻辑基本正确。
更多技术信息,包括操作系统,编译器,面试算法,机器学习,人工智能,请关照我的公众号: