netty代理,客户端发送post,put,delete请求

1 篇文章 0 订阅

POST请求

//url是访问此方法的请求路径
FullHttpRequest request = new DefaultFullHttpRequest(
      HttpVersion.HTTP_1_1, HttpMethod.POST, url); 

request.headers().add(HttpHeaderNames.HOST, host); 
//这一条可删除,加上有时候会报错
request.headers().add(HttpHeaderNames.CONNECTION, HttpHeaders.Values.KEEP_ALIVE); 
//这一条可删除,加上有时候会报错
request.headers().add(HttpHeaderNames.ACCEPT_ENCODING, HttpHeaders.Values.GZIP); 
ByteBuf bbuf = Unpooled.copiedBuffer("参数内容", StandardCharsets.UTF_8); 
request.headers().add(HttpHeaderNames.CONTENT_LENGTH, bbuf.readableBytes()); 
request.content().clear().writeBytes(bbuf); 
// Send the HTTP request. 
channel.writeAndFlush(request); 

PUT,DELETE请求,与post请求一样,需设置一个默认的Content type
request.headers().add(HttpHeaderNames.CONTENT_TYPE, “application/json”);

HttpMethod method = request.method();
request = new DefaultFullHttpRequest(
   HttpVersion.HTTP_1_1, method, realUri);
        if (!"POST".equals(method.toString())) {
             //设置默认Content type
             request.headers().add(HttpHeaderNames.CONTENT_TYPE, "application/json");
        }
        request.headers().add(HttpHeaderNames.HOST, host);
        ByteBuf buf = Unpooled.copiedBuffer("参数内容", StandardCharsets.UTF_8);
        request.headers().add(HttpHeaderNames.CONTENT_LENGTH, buf.readableBytes());
        request.content().clear().writeBytes(buf);
// Send the HTTP request. 
channel.writeAndFlush(request); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值