Netty HttpRequest 包括 Json
Netty HttpRequest including Json
我正在尝试构建 HTTP 请求到我们的 Rackspace CDN
我需要添加一个Json字符串作为内容来给出登录说明
我使用 curl 来测试信息并且它有效但是通过 Netty 我得到一个 400 Bad Request 响应告诉我我没有正确传递 Json Github 上的所有示例都过于复杂堆栈溢出中的那些是版本 3 任何帮助将不胜感激这是我的代码
我正在使用 Netty 4.1.0Beta5
URI uri = new URI("https://identity.api.rackspacecloud.com");
HttpDataFactory factory = new DefaultHttpDataFactory(
DefaultHttpDataFactory.MINSIZE);
HttpPostRequestEncoder bodyRequestEncoder = new
HttpPostRequestEncoder(factory, request, false);
HttpHeaders headers = request.headers();
headers.set(HttpHeaderNames.HOST,
"https://identity.api.rackspacecloud.com/v2.0/tokens");
headers.set(HttpHeaderNames.CONTENT_TYPE, new
AsciiString("application/json"));
String json = "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username
\":\"AAsdFrank\",\"apiKey\":\"adfadfdadsf33434fdfdfdfaf\"}}}";
ByteBuf buffer= Unpooled.copiedBuffer(json, CharsetUtil.UTF_8);
headers.set(HttpHeaderNames.CONTENT_LENGTH,
String.valueOf(buffer.readableBytes()));
bodyRequestEncoder.addBodyAttribute("json", json);
request = bodyRequestEncoder.finalizeRequest();
io.netty.channel.Channel channel =
cdnBootstrap.connect("identity.api.rackspacecloud.com",
443).sync().channel();
channel.writeAndFlush(request);
我收到以下响应,告诉我我已连接到服务器,但我的请求格式不正确
STATUS: 400 Bad Request
VERSION: HTTP/1.1
HEADER: Connection = close
HEADER: Content-Length = 166
HEADER: Content-Type = text/html
HEADER: Date = Thu, 25 Jun 2015 22:10:20 GMT
HEADER: Server = nginx
CONTENT {
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
欢迎大家提出建议
经过通常太多的时间后,我在这里找到了可以使用的解决方案
Netty 4.1.0.Beta5
请求组合
String json = "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username
\":\"\ADDHDD",\"apiKey\":\"adfasdfasdfasdfasdfasdfsdf\"}}}";
URI uri = new URI("https://identity.api.rackspacecloud.com/v2.0/tokens");
DefaultFullHttpRequest request = new
DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
uri.getRawPath());
request.headers().set(HttpHeaderNames.CONTENT_TYPE,
"application/json");
request.headers().set(HttpHeaderNames.ACCEPT_ENCODING,
HttpHeaderValues.GZIP);
request.headers().set(HttpHeaderNames.HOST,
"identity.api.rackspacecloud.com:443");
ByteBuf buffer = request.content().clear();
int p0 = buffer.writerIndex();
buffer.writeBytes(json.getBytes(CharsetUtil.UTF_8));
int p1 = buffer.writerIndex();
request.headers().set(HttpHeaderNames.CONTENT_LENGTH,
Integer.toString(p1 - p0));
io.netty.channel.Channel channel =
dnBootstrap.connect("identity.api.rackspacecloud.com",
443).sync().channel();
channel.writeAndFlush(request);
这是初始化程序
ChannelPipeline pipeline = ch.pipeline();
if (sslCtx != null) {
pipeline.addLast("ssl", sslCtx.newHandler(ch.alloc()));
}
pipeline.addLast("codec", new HttpClientCodec());
pipeline.addLast("inflater", new HttpContentDecompressor());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("ResponseDecoder",new HttpResponseDecoder());
pipeline.addLast("aggregator",new
HttpObjectAggregator(512*1024));
pipeline.addLast("handler", new CdnClientHandler());
我正在尝试构建 HTTP 请求到我们的 Rackspace CDN
我需要添加一个Json字符串作为内容来给出登录说明
我使用 curl 来测试信息并且它有效但是通过 Netty 我得到一个 400 Bad Request 响应告诉我我没有正确传递 Json Github 上的所有示例都过于复杂堆栈溢出中的那些是版本 3 任何帮助将不胜感激这是我的代码 我正在使用 Netty 4.1.0Beta5
URI uri = new URI("https://identity.api.rackspacecloud.com");
HttpDataFactory factory = new DefaultHttpDataFactory(
DefaultHttpDataFactory.MINSIZE);
HttpPostRequestEncoder bodyRequestEncoder = new
HttpPostRequestEncoder(factory, request, false);
HttpHeaders headers = request.headers();
headers.set(HttpHeaderNames.HOST,
"https://identity.api.rackspacecloud.com/v2.0/tokens");
headers.set(HttpHeaderNames.CONTENT_TYPE, new
AsciiString("application/json"));
String json = "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username
\":\"AAsdFrank\",\"apiKey\":\"adfadfdadsf33434fdfdfdfaf\"}}}";
ByteBuf buffer= Unpooled.copiedBuffer(json, CharsetUtil.UTF_8);
headers.set(HttpHeaderNames.CONTENT_LENGTH,
String.valueOf(buffer.readableBytes()));
bodyRequestEncoder.addBodyAttribute("json", json);
request = bodyRequestEncoder.finalizeRequest();
io.netty.channel.Channel channel =
cdnBootstrap.connect("identity.api.rackspacecloud.com",
443).sync().channel();
channel.writeAndFlush(request);
我收到以下响应,告诉我我已连接到服务器,但我的请求格式不正确
STATUS: 400 Bad Request
VERSION: HTTP/1.1
HEADER: Connection = close
HEADER: Content-Length = 166
HEADER: Content-Type = text/html
HEADER: Date = Thu, 25 Jun 2015 22:10:20 GMT
HEADER: Server = nginx
CONTENT {
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>
欢迎大家提出建议
经过通常太多的时间后,我在这里找到了可以使用的解决方案 Netty 4.1.0.Beta5 请求组合
String json = "{\"auth\":{\"RAX-KSKEY:apiKeyCredentials\":{\"username
\":\"\ADDHDD",\"apiKey\":\"adfasdfasdfasdfasdfasdfsdf\"}}}";
URI uri = new URI("https://identity.api.rackspacecloud.com/v2.0/tokens");
DefaultFullHttpRequest request = new
DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST,
uri.getRawPath());
request.headers().set(HttpHeaderNames.CONTENT_TYPE,
"application/json");
request.headers().set(HttpHeaderNames.ACCEPT_ENCODING,
HttpHeaderValues.GZIP);
request.headers().set(HttpHeaderNames.HOST,
"identity.api.rackspacecloud.com:443");
ByteBuf buffer = request.content().clear();
int p0 = buffer.writerIndex();
buffer.writeBytes(json.getBytes(CharsetUtil.UTF_8));
int p1 = buffer.writerIndex();
request.headers().set(HttpHeaderNames.CONTENT_LENGTH,
Integer.toString(p1 - p0));
io.netty.channel.Channel channel =
dnBootstrap.connect("identity.api.rackspacecloud.com",
443).sync().channel();
channel.writeAndFlush(request);
这是初始化程序
ChannelPipeline pipeline = ch.pipeline();
if (sslCtx != null) {
pipeline.addLast("ssl", sslCtx.newHandler(ch.alloc()));
}
pipeline.addLast("codec", new HttpClientCodec());
pipeline.addLast("inflater", new HttpContentDecompressor());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("ResponseDecoder",new HttpResponseDecoder());
pipeline.addLast("aggregator",new
HttpObjectAggregator(512*1024));
pipeline.addLast("handler", new CdnClientHandler());