java实现HTTP协议:POST协议代码实现
Posted Coding迪斯尼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java实现HTTP协议:POST协议代码实现相关的知识,希望对你有一定的参考价值。
package Application;
import java.util.Arrays;
public class MIMETextPlainMediaEcnapusulation { //该类只封装简单的文本数据
private String boundary_string = "----WebFormBoundaryAAABBBCCCDDD"; //用于传递数据的分割标志字符串
private String post_file_name = "";
private String content_part_header = "Content-Disposition: form-data; name=\"button\"; filename=\"";
private String content_type = "Content-Type: text/plain\r\n\r\n";
private String content_part = "";
private String last_boundary = "\r\n--" + boundary_string + "--\r\n";
public MIMETextPlainMediaEcnapusulation(String file_name) {
this.post_file_name = file_name; //要上传的文件名
}
private void add_content_header() { //添加用于分割不同数据部分的boundary
this.content_part += "--";
this.content_part += boundary_string;
this.content_part += "\r\n";
this.content_part += content_part_header;
this.content_part += post_file_name;
this.content_part += "\r\n";
this.content_part += content_type;
}
public void add_content(String content) { //调用该接口设置要上传给服务器的内容
this.add_content_header();
this.content_part += content;
this.content_part += "\r\n";
this.content_part += "--";
this.content_part += boundary_string;
this.content_part += "\r\n";
}
private void add_last_content_part() { //模拟最后upload数据部分,这部分与数据传输无关,但与服务器对接收数据的解读有关
String last_content_disposition = "Content-Disposition: form-data; name=\"button\"\r\n\r\n";
content_part += last_content_disposition;
String content = "Upload\r\n";
content_part += content;
content_part += last_boundary;
}
public String get_mime_part() {
this.add_last_content_part();
return content_part;
}
public String get_boundary_string() {
return boundary_string;
}
}
package Application;
import java.net.InetAddress;
import utils.ITCPHandler;
public class HTTPPostClient 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 String accept_encoding = "gzip, deflate";
private String accept_language = "zh-CN,zh;q=0.9,en;q=0.8";
private String cache_control = "max-age=0";
private String connection = "close";
private static int HTTP_OK = 200;
private String file_name = "my_test.txt";
private String content_type = "multipart/form-data; boundary=";
private MIMETextPlainMediaEcnapusulation mime_encoded = new MIMETextPlainMediaEcnapusulation(file_name);
private void send_content() throws Exception {//设置http请求数据的头部信息
mime_encoded.add_content("This is test content for line1"); //文档里面的内容
String send_content = mime_encoded.get_mime_part();
String content_length = Integer.toString(send_content.length());
httpEncoder.set_method(HTTPEncoder.HTTP_METHOD.HTTP_POST, "/");
httpEncoder.set_header("Host","192.168.2.127:8888");
httpEncoder.set_header("Connection", connection);
httpEncoder.set_header("User-Agent", user_agent);
httpEncoder.set_header("Content-Length", content_length);
httpEncoder.set_header("Accept", accept);
httpEncoder.set_header("Accept-Encoding", accept_encoding);
httpEncoder.set_header("Accept-Language", accept_language);
httpEncoder.set_header("Cache-Control", cache_control);
httpEncoder.set_header("Content-type", content_type + mime_encoded.get_boundary_string());
httpEncoder.set_header("Upgrade-Insecure-Requests", "1");
httpEncoder.set_header("Origin", "http://192.168.2.127:8888");
httpEncoder.set_header("Referer", "http://192.168.2.127:8888/");
String http_content = httpEncoder.get_http_content();
http_content += send_content;
System.out.println(http_content);
byte[] send_content_bytes = http_content.getBytes();
tcp_socket.tcp_send(send_content_bytes);
}
@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);
int code = httpEncoder.get_return_code(content);
if (code != HTTP_OK) {
System.out.println("http return error: " + code);
} else {
System.out.println("Http return 200 OK! Post Success!");
}
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();
}
}
}
以上是关于java实现HTTP协议:POST协议代码实现的主要内容,如果未能解决你的问题,请参考以下文章
JAVA 怎么实现HTTP的POST方式通讯,以及HTTPS方式传递
一个linux下简单的纯C++实现Http请求类(GET,POST,上传,下载)