溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Netty4之怎么實(shí)現(xiàn)HTTP請(qǐng)求、響應(yīng)

發(fā)布時(shí)間:2023-04-04 10:44:52 來(lái)源:億速云 閱讀:165 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Netty4之怎么實(shí)現(xiàn)HTTP請(qǐng)求、響應(yīng)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Netty4之怎么實(shí)現(xiàn)HTTP請(qǐng)求、響應(yīng)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

1.Netty中HTTP請(qǐng)求和響應(yīng)類

請(qǐng)求(FullHttpRequest)

/**
 * Combine the {@link HttpRequest} and {@link FullHttpMessage}, so the request is a <i>complete</i> HTTP
 * request.
 */
public interface FullHttpRequest extends HttpRequest, FullHttpMessage {

可以看到,它結(jié)合了HttpRequest、FullHttpMessag,作為一個(gè)完整的HTTP請(qǐng)求體。

默認(rèn)實(shí)現(xiàn)為DefaultFullHttpRequest

響應(yīng)(FullHttpResponse)

/**
 * Combination of a {@link HttpResponse} and {@link FullHttpMessage}.
 * So it represent a <i>complete</i> http response.
 */
public interface FullHttpResponse extends HttpResponse, FullHttpMessage {

同樣,它結(jié)合了HttpResponse、FullHttpMessage

默認(rèn)實(shí)現(xiàn)為DefaultFullHttpResponse

*

2.Netty中客戶端、服務(wù)端的編解碼器

作為服務(wù)端而言:

主要工作就是接收客戶端請(qǐng)求,將客戶端的請(qǐng)求內(nèi)容解碼;發(fā)送響應(yīng)給客戶端,并將發(fā)送內(nèi)容編碼

所以,服務(wù)端需要兩個(gè)編解碼器

* HttpRequestDecoder(將請(qǐng)求內(nèi)容解碼)

* HttpResponseEncoder(將響應(yīng)內(nèi)容編碼)

作為客戶端而言:

主要工作就是發(fā)送請(qǐng)求給服務(wù)端,并將發(fā)送內(nèi)容編碼;接收服務(wù)端響應(yīng),并將接收內(nèi)容解碼;

所以,客戶端需要兩個(gè)編解碼器

* HttpResponseDecoder(將響應(yīng)內(nèi)容解碼)

* HttpRequestEncoder(將請(qǐng)求內(nèi)容編碼)

3.Server端編寫Handler類處理客戶請(qǐng)求

創(chuàng)建Handler,命名為HttpHandler,具體內(nèi)容如下:

import com.alibaba.fastjson.JSONObject;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;
import lombok.Data;

/**
 * 處理HTTP請(qǐng)求
 * @author Administrator
 *
 */
public class HttpHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
	
		if(msg instanceof FullHttpRequest){
			
			FullHttpRequest req = (FullHttpRequest)msg;
			
			try {
				
				// 1.獲取URI
				String uri = req.uri();
				
				// 2.獲取請(qǐng)求體
				ByteBuf buf = req.content();
				String content = buf.toString(CharsetUtil.UTF_8);
				
				// 3.獲取請(qǐng)求方法
				HttpMethod method = req.method();
				
				// 4.獲取請(qǐng)求頭
				HttpHeaders headers = req.headers();
				
				// 5.根據(jù)method,確定不同的邏輯
				if(method.equals(HttpMethod.GET)){
					
					// TODO 
				}
				
				if(method.equals(HttpMethod.POST)){
					// 接收用戶輸入,并將輸入返回給用戶
					Content c = new Content();
					c.setUri(uri);
					c.setContent(content);
					
					response(ctx, c);
				}
				
				if(method.equals(HttpMethod.PUT)){
					// TODO 
				}
				
				if(method.equals(HttpMethod.DELETE)){
					// TODO 
				}
			} finally {
				req.release();
			}
		}
	}

	private void response(ChannelHandlerContext ctx, Content c) {

		// 1.設(shè)置響應(yīng)
		FullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
				HttpResponseStatus.OK, 
				Unpooled.copiedBuffer(JSONObject.toJSONString(c), CharsetUtil.UTF_8));
		
		resp.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
		
		// 2.發(fā)送
		// 注意必須在使用完之后,close channel
		ctx.writeAndFlush(resp).addListener(ChannelFutureListener.CLOSE);
	}
}

@Data
class Content{
	String uri;
	String content;
}

注意:

在處理過(guò)程中,把msg轉(zhuǎn)換為FullHttpRequest,可以獲取關(guān)于請(qǐng)求的所有內(nèi)容;

在發(fā)送響應(yīng)時(shí)必須要監(jiān)聽CLOSE

*

4.測(cè)試

啟動(dòng)Server類使用客戶端發(fā)送請(qǐng)求

在這里,筆者不單獨(dú)編寫Netty客戶端代碼,直接使用PostMan來(lái)充當(dāng)客戶端發(fā)送請(qǐng)求,具體如下:

Netty4之怎么實(shí)現(xiàn)HTTP請(qǐng)求、響應(yīng)

發(fā)送一個(gè)post請(qǐng)求,并填寫body,點(diǎn)擊send,可以看到響應(yīng)如下所示:

Netty4之怎么實(shí)現(xiàn)HTTP請(qǐng)求、響應(yīng)

讀到這里,這篇“Netty4之怎么實(shí)現(xiàn)HTTP請(qǐng)求、響應(yīng)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI