zoukankan      html  css  js  c++  java
  • java网络访问指定出口ip

    java网络访问指定出口ip

    Table of Contents

    1 socket

    可以在Socket构造函数中指定使用的本地ip,如:

    Socket socket = new Socket("127.0.0.1", 12345, InetAddress.getByAddress(new byte[] {
                    new Integer(10).byteValue(),
                    new Integer(211).byteValue(),
                    new Integer(55).byteValue(),
                    new Integer(2).byteValue(),
            }), 1234);
    

    2 apache httpclient

    通过HttpClient设置即可,如下:

    byte b[] = new byte[4];
    b[0] = new Integer(10).byteValue();
    b[1] = new Integer(211).byteValue();
    b[2] = new Integer(55).byteValue();
    b[3] = new Integer(2).byteValue();
    
    CloseableHttpResponse response = null;
    CloseableHttpClient httpclient = HttpClients.createDefault();
    
    HttpGet httpget = new HttpGet("http://localhost:8080/TestServlet");
    httpget.setConfig(RequestConfig.custom()
            .setLocalAddress(InetAddress.getByAddress(b))
            .build());
    

    Author: galaxy

    Created: 2016-08-31 Wed 17:11

    Emacs 24.5.6 (Org mode 8.2.10)

    Validate

  • 相关阅读:
    数据类型
    注释
    编译型语言和解释型语言
    POJ1026 Cipher(置换的幂运算)
    最短路(代码来源于kuangbin和百度)
    POJ1753 Flip Game(bfs、枚举)
    POJ1860 Currency Exchange(bellman-ford)
    【转】博弈—SG函数
    【转】欧几里得与扩展欧几里得
    HDU 5833 Zhu and 772002(高斯消元)
  • 原文地址:https://www.cnblogs.com/galaxy-gao/p/5826791.html
Copyright © 2011-2022 走看看