问题
I am using netty http client to fetch urls using netty. However, for some urls which are redirecting to some other page, I am unable to fetch the content of the final page using my client. I want to know how to handle 302 redirects in my response handler.
Below is the code used in messageReceived function of my response handler.
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (!readingChunks) { HttpResponse response = (HttpResponse) e.getMessage(); System.out.println("STATUS: " + response.getStatus()); System.out.println("VERSION: " + response.getProtocolVersion()); System.out.println(); if (!response.getHeaderNames().isEmpty()) { for (String name: response.getHeaderNames()) { for (String value: response.getHeaders(name)) { System.out.println("HEADER: " + name + " = " + value); } } System.out.println(); } if (response.isChunked()) { readingChunks = true; System.out.println("CHUNKED CONTENT {"); } else { ChannelBuffer content = response.getContent(); if (content.readable()) { System.out.println("CONTENT {"); System.out.println(content.toString(CharsetUtil.UTF_8)); System.out.println("} END OF CONTENT"); } } } else { HttpChunk chunk = (HttpChunk) e.getMessage(); if (chunk.isLast()) { readingChunks = false; System.out.println("} END OF CHUNKED CONTENT"); } else { System.out.print(chunk.getContent().toString(CharsetUtil.UTF_8)); System.out.flush(); } } }
来源:https://stackoverflow.com/questions/11364379/handling-http-302-moved-temporarily-requests-in-netty