国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 使用 Java 類 實(shí)現(xiàn)Http協(xié)議

使用 Java 類 實(shí)現(xiàn)Http協(xié)議

2021-12-21 13:43冰 河 Java教程

這篇文章主要介紹了用幾個(gè)Java類簡(jiǎn)單的實(shí)現(xiàn)了Http協(xié)議相關(guān)資料,感興趣的的朋友可以參考下面具體的文章內(nèi)容

Java 實(shí)現(xiàn)Http協(xié)議

HTTP協(xié)議屬于應(yīng)用層協(xié)議,它構(gòu)建于TCP和IP協(xié)議之上,處于TCP/IP協(xié)議架構(gòu)層的頂端,所以,它不用處理下層協(xié)議間諸如丟包補(bǔ)發(fā)、握手及數(shù)據(jù)的分段及重新組裝等繁瑣的細(xì)節(jié),使開(kāi)發(fā)人員可以專注于應(yīng)用業(yè)務(wù)。

協(xié)議是通信的規(guī)范,為了更好的理解HTTP協(xié)議,我們可以基于Java的Socket API接口,通過(guò)設(shè)計(jì)一個(gè)簡(jiǎn)單的應(yīng)用層通信協(xié)議,來(lái)簡(jiǎn)單分析下協(xié)議實(shí)現(xiàn)的過(guò)程和細(xì)節(jié)。

在我們今天的示例程序中,客戶端會(huì)向服務(wù)端發(fā)送一條命令,服務(wù)端在接收到命令后,會(huì)判斷命令是否是“HELLO”,如果是“HELLO”, 則服務(wù)端返回給客戶端的響應(yīng)為“hello”,否則,服務(wù)端返回給客戶端的響應(yīng)為“bye bye”。

我們接下來(lái)用Java實(shí)現(xiàn)這個(gè)簡(jiǎn)單的應(yīng)用層通信協(xié)議,

使用 Java 類 實(shí)現(xiàn)Http協(xié)議

一、協(xié)議請(qǐng)求的定義

協(xié)議的請(qǐng)求主要包括:編碼、命令和命令長(zhǎng)度三個(gè)字段。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.binghe.params;
/**
 * 協(xié)議請(qǐng)求的定義
 * @author binghe
 *
 */
public class Request {
 /**
  * 協(xié)議編碼
  */
 private byte encode;
 
 /**
  * 命令
  */
 private String command;
 
 /**
  * 命令長(zhǎng)度
  */
 private int commandLength;
 
 public Request() {
  super();
 }
 
 public Request(byte encode, String command, int commandLength) {
  super();
  this.encode = encode;
  this.command = command;
  this.commandLength = commandLength;
 }
 
 public byte getEncode() {
  return encode;
 }
 
 public void setEncode(byte encode) {
  this.encode = encode;
 }
 
 public String getCommand() {
  return command;
 }
 
 public void setCommand(String command) {
  this.command = command;
 }
 
 public int getCommandLength() {
  return commandLength;
 }
 
 public void setCommandLength(int commandLength) {
  this.commandLength = commandLength;
 }
 
 @Override
 public String toString() {
  return "Request [encode=" + encode + ", command=" + command
    + ", commandLength=" + commandLength + "]";
 }
 
}

二、響應(yīng)協(xié)議的定義

協(xié)議的響應(yīng)主要包括:編碼、響應(yīng)內(nèi)容和響應(yīng)長(zhǎng)度三個(gè)字段。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.binghe.params;
 
/**
 * 協(xié)議響應(yīng)的定義
 * @author binghe
 *
 */
public class Response {
 /**
  * 編碼
  */
 private byte encode;
 
 /**
  * 響應(yīng)內(nèi)容
  */
 private String response;
 
 /**
  * 響應(yīng)長(zhǎng)度
  */
 private int responseLength;
 
 public Response() {
  super();
 }
 
 public Response(byte encode, String response, int responseLength) {
  super();
  this.encode = encode;
  this.response = response;
  this.responseLength = responseLength;
 }
 
 public byte getEncode() {
  return encode;
 }
 
 public void setEncode(byte encode) {
  this.encode = encode;
 }
 
 public String getResponse() {
  return response;
 }
 
 public void setResponse(String response) {
  this.response = response;
 }
 
 public int getResponseLength() {
  return responseLength;
 }
 
 public void setResponseLength(int responseLength) {
  this.responseLength = responseLength;
 }
 
 @Override
 public String toString() {
  return "Response [encode=" + encode + ", response=" + response
    + ", responseLength=" + responseLength + "]";
 }
 
}

三、編碼常量定義

編碼常量的定義主要包括UTF-8和GBK兩種編碼。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.binghe.constant;
 
/**
 * 常量類
 * @author binghe
 *
 */
public final class Encode {
 //UTF-8編碼
 public static final byte UTF8 = 1;
 //GBK編碼
 public static final byte GBK = 2;
}

四、客戶端的實(shí)現(xiàn)

客戶端先構(gòu)造一個(gè)request請(qǐng)求,通過(guò)Socket接口將其發(fā)送到遠(yuǎn)端,并接收遠(yuǎn)端的響應(yīng)信息,并構(gòu)造成一個(gè)Response對(duì)象。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.binghe.protocol.client;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
 
import com.binghe.constant.Encode;
import com.binghe.params.Request;
import com.binghe.params.Response;
import com.binghe.utils.ProtocolUtils;
 
/**
 * 客戶端代碼
 * @author binghe
 *
 */
public final class Client {
 public static void main(String[] args) throws IOException{
  //請(qǐng)求
  Request request = new Request();
  request.setCommand("HELLO");
  request.setCommandLength(request.getCommand().length());
  request.setEncode(Encode.UTF8);
  
  Socket client = new Socket("127.0.0.1", 4567);
  OutputStream out = client.getOutputStream();
  
  //發(fā)送請(qǐng)求
  ProtocolUtils.writeRequest(out, request);
  
  //讀取響應(yīng)數(shù)據(jù)
  InputStream in = client.getInputStream();
  Response response = ProtocolUtils.readResponse(in);
  System.out.println("獲取的響應(yīng)結(jié)果信息為: " + response.toString());
 }
}

五、服務(wù)端的實(shí)現(xiàn)

服務(wù)端接收客戶端的請(qǐng)求,根據(jù)接收命令的不同,響應(yīng)不同的消息信息,如果是“HELLO”命令,則響應(yīng)“hello”信息,否則響應(yīng)“bye bye”信息。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.binghe.protocol.server;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
 
import com.binghe.constant.Encode;
import com.binghe.params.Request;
import com.binghe.params.Response;
import com.binghe.utils.ProtocolUtils;
 
/**
 * Server端代碼
 * @author binghe
 *
 */
public final class Server {
 public static void main(String[] args) throws IOException{
  ServerSocket server = new ServerSocket(4567);
  while (true) {
   Socket client = server.accept();
   //讀取請(qǐng)求數(shù)據(jù)
   InputStream input = client.getInputStream();
   Request request = ProtocolUtils.readRequest(input);
   System.out.println("收到的請(qǐng)求參數(shù)為: " + request.toString());
   OutputStream out = client.getOutputStream();
   //組裝響應(yīng)數(shù)據(jù)
   Response response = new Response();
   response.setEncode(Encode.UTF8);
   if("HELLO".equals(request.getCommand())){
    response.setResponse("hello");
   }else{
    response.setResponse("bye bye");
   }
   response.setResponseLength(response.getResponse().length());
   ProtocolUtils.writeResponse(out, response);
  }
 }
}

六、ProtocolUtils工具類的實(shí)現(xiàn)

ProtocolUtilsreadRequest方法將從傳遞進(jìn)來(lái)的輸入流中讀取請(qǐng)求的encodecommandcommandLength三個(gè)參數(shù),進(jìn)行相應(yīng)的編碼轉(zhuǎn)化,構(gòu)造成Request對(duì)象返回。而writeResponse方法則是將response對(duì)象的字段根據(jù)對(duì)應(yīng)的編碼寫(xiě)入到響應(yīng)的輸出流中。

有一個(gè)細(xì)節(jié)需要重點(diǎn)注意:OutputStream中直接寫(xiě)入一個(gè)int類型,會(huì)截取其低8位,丟棄其高24位,所以,在傳遞和接收數(shù)據(jù)時(shí),需要進(jìn)行相應(yīng)的轉(zhuǎn)化操作。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package com.binghe.utils;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
 
import com.binghe.constant.Encode;
import com.binghe.params.Request;
import com.binghe.params.Response;
 
/**
 * 協(xié)議工具類
 * @author binghe
 *
 */
public final class ProtocolUtils {
 /**
  * 從輸入流中反序列化Request對(duì)象
  * @param input
  * @return
  * @throws IOException
  */
 public static Request readRequest(InputStream input) throws IOException{
  //讀取編碼
  byte[] encodeByte = new byte[1];
  input.read(encodeByte);
  byte encode = encodeByte[0];
  
  //讀取命令長(zhǎng)度
  byte[] commandLengthBytes = new byte[4];
  input.read(commandLengthBytes);
  int commandLength = ByteUtils.byte2Int(commandLengthBytes);
  
  //讀取命令
  byte[] commandBytes = new byte[commandLength];
  input.read(commandBytes);
  String command = "";
  if(Encode.UTF8 == encode){
   command = new String(commandBytes, "UTF-8");
  }else if(Encode.GBK == encode){
   command = new String(commandBytes, "GBK");
  }
  //組裝請(qǐng)求返回
  Request request = new Request(encode, command, commandLength);
  return request;
 }
 /**
  * 從輸入流中反序列化Response對(duì)象
  * @param input
  * @return
  * @throws IOException
  */
 public static Response readResponse(InputStream input) throws IOException{
  //讀取編碼
  byte[] encodeByte = new byte[1];
  input.read(encodeByte);
  byte encode = encodeByte[0];
  
  //讀取響應(yīng)長(zhǎng)度
  byte[] responseLengthBytes = new byte[4];
  input.read(responseLengthBytes);
  int responseLength = ByteUtils.byte2Int(responseLengthBytes);
  
  //讀取命令
  byte[] responseBytes = new byte[responseLength];
  input.read(responseBytes);
  String response = "";
  if(Encode.UTF8 == encode){
   response = new String(responseBytes, "UTF-8");
  }else if(Encode.GBK == encode){
   response = new String(responseBytes, "GBK");
  }
  //組裝請(qǐng)求返回
  Response resp = new Response(encode, response, responseLength);
  return resp;
 }
 
 /**
  * 序列化請(qǐng)求信息
  * @param output
  * @param response
  */
 public static void writeRequest(OutputStream output, Request request) throws IOException{
  //將response響應(yīng)返回給客戶端
  output.write(request.getEncode());
  //output.write(response.getResponseLength());直接write一個(gè)int類型會(huì)截取低8位傳輸丟棄高24位
  output.write(ByteUtils.int2ByteArray(request.getCommandLength()));
  if(Encode.UTF8 == request.getEncode()){
   output.write(request.getCommand().getBytes("UTF-8"));
  }else if(Encode.GBK == request.getEncode()){
   output.write(request.getCommand().getBytes("GBK"));
  }
  output.flush();
 }
 /**
  * 序列化響應(yīng)信息
  * @param output
  * @param response
  */
 public static void writeResponse(OutputStream output, Response response) throws IOException{
  //將response響應(yīng)返回給客戶端
  output.write(response.getEncode());
  //output.write(response.getResponseLength());直接write一個(gè)int類型會(huì)截取低8位傳輸丟棄高24位
  output.write(ByteUtils.int2ByteArray(response.getResponseLength()));
  if(Encode.UTF8 == response.getEncode()){
   output.write(response.getResponse().getBytes("UTF-8"));
  }else if(Encode.GBK == response.getEncode()){
   output.write(response.getResponse().getBytes("GBK"));
  }
  output.flush();
 }
}

七、ByteUtils類的實(shí)現(xiàn)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.binghe.utils;
 
/**
 * 字節(jié)轉(zhuǎn)化工具類
 * @author binghe
 *
 */
public final class ByteUtils {
 /**
  * 將byte數(shù)組轉(zhuǎn)化為int數(shù)字
  * @param bytes
  * @return
  */
 public static int byte2Int(byte[] bytes){
  int num = bytes[3] & 0xFF;
  num |= ((bytes[2] << 8) & 0xFF00);
  num |= ((bytes[1] << 16) & 0xFF0000);
  num |= ((bytes[0] << 24) & 0xFF000000);
  return num;
 }
 
 /**
  * 將int類型數(shù)字轉(zhuǎn)化為byte數(shù)組
  * @param num
  * @return
  */
 public static byte[] int2ByteArray(int i){
  byte[] result = new byte[4];
  result[0]  = (byte)(( i >> 24 ) & 0xFF);
  result[1]  = (byte)(( i >> 16 ) & 0xFF);
  result[2]  = (byte)(( i >> 8 ) & 0xFF);
  result[3]  = (byte)(i & 0xFF);
  return result;
 }
}

到此這篇關(guān)于關(guān)于Java 實(shí)現(xiàn)Http協(xié)議詳細(xì)內(nèi)容的文章就介紹到這了,更多相關(guān)Java 實(shí)現(xiàn)Http協(xié)議內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!

使用 Java 類 實(shí)現(xiàn)Http協(xié)議

原文鏈接:https://blog.csdn.net/l1028386804/article/details/117154518

延伸 · 閱讀

精彩推薦
  • Java教程小米推送Java代碼

    小米推送Java代碼

    今天小編就為大家分享一篇關(guān)于小米推送Java代碼,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧...

    富貴穩(wěn)中求8032021-07-12
  • Java教程20個(gè)非常實(shí)用的Java程序代碼片段

    20個(gè)非常實(shí)用的Java程序代碼片段

    這篇文章主要為大家分享了20個(gè)非常實(shí)用的Java程序片段,對(duì)java開(kāi)發(fā)項(xiàng)目有所幫助,感興趣的小伙伴們可以參考一下 ...

    lijiao5352020-04-06
  • Java教程Java8中Stream使用的一個(gè)注意事項(xiàng)

    Java8中Stream使用的一個(gè)注意事項(xiàng)

    最近在工作中發(fā)現(xiàn)了對(duì)于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個(gè)非常重要的注意點(diǎn),所以這篇文章主要給大家介紹了關(guān)于Java8中S...

    阿杜7482021-02-04
  • Java教程Java BufferWriter寫(xiě)文件寫(xiě)不進(jìn)去或缺失數(shù)據(jù)的解決

    Java BufferWriter寫(xiě)文件寫(xiě)不進(jìn)去或缺失數(shù)據(jù)的解決

    這篇文章主要介紹了Java BufferWriter寫(xiě)文件寫(xiě)不進(jìn)去或缺失數(shù)據(jù)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望...

    spcoder14552021-10-18
  • Java教程Java使用SAX解析xml的示例

    Java使用SAX解析xml的示例

    這篇文章主要介紹了Java使用SAX解析xml的示例,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下...

    大行者10067412021-08-30
  • Java教程xml與Java對(duì)象的轉(zhuǎn)換詳解

    xml與Java對(duì)象的轉(zhuǎn)換詳解

    這篇文章主要介紹了xml與Java對(duì)象的轉(zhuǎn)換詳解的相關(guān)資料,需要的朋友可以參考下...

    Java教程網(wǎng)2942020-09-17
  • Java教程Java實(shí)現(xiàn)搶紅包功能

    Java實(shí)現(xiàn)搶紅包功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)搶紅包功能,采用多線程模擬多人同時(shí)搶紅包,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙...

    littleschemer13532021-05-16
  • Java教程升級(jí)IDEA后Lombok不能使用的解決方法

    升級(jí)IDEA后Lombok不能使用的解決方法

    最近看到提示IDEA提示升級(jí),尋思已經(jīng)有好久沒(méi)有升過(guò)級(jí)了。升級(jí)完畢重啟之后,突然發(fā)現(xiàn)好多錯(cuò)誤,本文就來(lái)介紹一下如何解決,感興趣的可以了解一下...

    程序猿DD9332021-10-08
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 一级免费av | 欧美第一专区 | 日韩中文字幕一区二区 | 中文字幕亚洲欧美 | 欧美午夜影院 | 亚洲欧美在线观看 | 欧美自拍视频 | 久久美女视频 | 天堂在线免费视频 | 黄色a级 | 美足av| 都市激情av| 国产黄 | 亚洲精品片 | 亚洲视频在线视频 | 亚洲欧美日韩国产综合 | 亚洲国产精品一区二区第一页 | 动漫精品一区二区 | 欧美在线综合 | 国产亚洲精品久久久久久 | 久久久久久国产一级毛片高清版 | 天堂资源最新在线 | 免费在线成人 | 人人干人人草 | 骚视频网站 | 2018啪一啪| 欧美资源在线 | 就操成人网| 日韩精品在线观看视频 | 欧美视频区 | 欧美综合网| 欧美三级在线播放 | 99re在线播放视频 | 日本精品久久久 | 亚洲精品国产成人 | 国产精品99久久免费观看 | 亚洲精品视频在线 | 黄色午夜 | 精品国产成人在线 | 国产精品成人一区二区三区 | 中文字幕视频一区 |