我正在使用 netty http 客户端使用 netty 获取 url。但是,对于一些重定向到其他页面的 url,我无法使用我的客户端获取最终页面的内容。我想知道如何在我的响应处理程序中处理 302 重定向。
下面是我的响应处理程序的 messageReceived 函数中使用的代码。
@覆盖
公共无效消息接收(ChannelHandlerContext ctx,MessageEvent e)抛出异常{
如果(!readingChunks){
HttpResponse 响应 = (HttpResponse) e.getMessage();
System.out.println("状态:" + response.getStatus());
System.out.println("版本:" + response.getProtocolVersion());
System.out.println();
if (!response.getHeaderNames().isEmpty()) {
for(字符串名称:response.getHeaderNames()){
for(字符串值:response.getHeaders(名称)){
System.out.println("标题:" + name + " = " + value);
}
}
System.out.println();
}
if (response.isChunked()) {
阅读大块=真;
System.out.println("分块内容 {");
} 别的 {
ChannelBuffer 内容 = response.getContent();
如果(内容。可读()){
System.out.println("内容{");
System.out.println(content.toString(CharsetUtil.UTF_8));
System.out.println("} 内容结束");
}
}
} 别的 {
HttpChunk 块 = (HttpChunk) e.getMessage();
if (chunk.isLast()) {
阅读大块=假;
System.out.println("} 块内容结束");
} 别的 {
System.out.print(chunk.getContent().toString(CharsetUtil.UTF_8));
System.out.flush();
}
}
}