2016-01-31 2 views
6

में सॉक्स 4/5 प्रॉक्सी हैंडलर का उपयोग कैसे करें मुझे नेटटी क्लाइंट में सॉक्स प्रॉक्सी कॉन्फ़िगर करने की आवश्यकता है (मोजे 4 या 5 प्रॉक्सी के माध्यम से विभिन्न साइटों का अनुरोध करने के लिए)। (, www.socks-proxy.net तरह http://sockslist.net/ आदि) मुक्त मोज़े सूची से लेकिन कोई भाग्य के साथ प्रॉक्सी का एक बहुत कोशिश की:नेटी क्लाइंट (4.1)

@Test 
public void testProxy() throws Exception { 
    final String ua = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"; 
    final String host = "www.main.de"; 
    final int port = 80; 

    Bootstrap b = new Bootstrap(); 
    b.group(new NioEventLoopGroup()) 
      .channel(NioSocketChannel.class) 
      .handler(new ChannelInitializer<SocketChannel>() { 
       @Override 
       protected void initChannel(SocketChannel ch) throws Exception { 
        ChannelPipeline p = ch.pipeline(); 

        p.addLast(new HttpClientCodec()); 
        p.addLast(new HttpContentDecompressor()); 
        p.addLast(new HttpObjectAggregator(10_485_760)); 
        p.addLast(new ChannelInboundHandlerAdapter() { 
         @Override 
         public void channelActive(final ChannelHandlerContext ctx) throws Exception { 
          HttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/"); 
          request.headers().set(HOST, host + ":" + port); 
          request.headers().set(USER_AGENT, ua); 
          request.headers().set(CONNECTION, CLOSE); 

          ctx.writeAndFlush(request); 

          System.out.println("!sent"); 
         } 

         @Override 
         public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { 
          System.out.println("!answer"); 
          if (msg instanceof FullHttpResponse) { 
           FullHttpResponse httpResp = (FullHttpResponse) msg; 


           ByteBuf content = httpResp.content(); 
           String strContent = content.toString(UTF_8); 
           System.out.println("body: " + strContent); 

           finish.countDown(); 
           return; 
          } 

          super.channelRead(ctx, msg); 
         } 

         @Override 
         public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 
          cause.printStackTrace(System.err); 
          ctx.close(); 
          finish.countDown(); 
         } 
        }); 

        p.addLast(new Socks4ProxyHandler(new InetSocketAddress("149.202.68.167", 37678))); 
       } 
      }); 

    b.connect(host, port).awaitUninterruptibly(); 
    System.out.println("!connected"); 

    finish.await(1, MINUTES); 
} 

कनेक्शन रुक जाता है, फिर सेट करता है या कुछ अजीब अपवाद हो रही। क्या गलत है? प्रॉक्सी समर्थन 4.1 से नेटटी में जोड़ा गया है (अब एक 4.1 सीआर है, इसे आजमाया गया है और 4.1 बी 7-8 पहले)

उत्तर

5

प्रॉक्सी इंस्टेंस पाइपलाइन में पहला होना चाहिए, क्योंकि आप इसे कनेक्शन को संभालना चाहते हैं प्रॉक्सी पहले, किसी भी http सामग्री को संभालने से पहले।

इसे बदलने के लिए, बदलने p.addLast(new Socks4ProxyHandler(new InetSocketAddress("149.202.68.167", 37678))); रहे हैं:

p.addFirst(new Socks4ProxyHandler(new InetSocketAddress("149.202.68.167", 37678))); 

रूप ChannelPipeline के लिए दस्तावेज में बताया गया है, डेटा का प्रवाह पहले हैंडलर पर शुरू कर रहा है, और पिछले हैंडलर पर समाप्त।

+0

ठीक है अगर पहले, धन्यवाद! – yetanothercoder

संबंधित मुद्दे