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);