我通过两个接口拥有对GNU/Linux服务器的root访问权限。我正在使用Jericho和HtmlUnit来抓取一些网页。我想指定哪个接口应该用于HTTP连接。有没有办法在命令行或Java中做到这一点?
发布于 2013-06-19 10:01:49
HTTP我想指定哪个接口应该用于
连接。
每个网络接口都有自己的唯一IP地址。您可以通过连接到接口的IP地址上的端口来绑定到接口。
我不能访问套接字。我给了HtmlUnit一个网址。
无论您是使用套接字(通过Socket / InetAddress)还是HTTP连接(通过URL),都使用相同的方法-绑定到IP地址+端口。
确定哪个接口具有哪个地址的
ip a ip link show /sbin/ifconfig -a lshw -class network
java.net.NetworkInterface类):导入java.io.*;导入java.net.*;导入java.util.*;导入静态java.lang.System.out;公共类ListNIFs { public static void main(String args[])抛出SocketException {枚举网络= NetworkInterface.getNetworkInterfaces();for (NetworkInterface netIf : Collections.list( nets )) {out.printf(“显示名称:%s\n",netIf.getDisplayName());Out.printf(“名称:%s\n",netIf.getName());枚举inetAddresses = netIf.getInetAddresses();for (InetAddress add : Collections.list(inetAddresses)) { out.printf("Address:%s\n",add.getName().getAddress().getHostAddress());} displaySubInterfaces(netIf);out.printf("\n");}}静态无效displaySubInterfaces(NetworkInterface netIf)抛出SocketException {枚举subIfs = netIf.getSubInterfaces();for (NetworkInterface subIf : Collections.list(subIfs)) { out.printf("\tSub接口显示名称:%s\n",subIf.getDisplayName());out.printf("\tSub接口名称:%s\n",subIf.getName());枚举inetAddresses = subIf.getInetAddresses();for (InetAddress add : Collections.list(inetAddresses)) {out.printf(“\t子接口地址:%s\n",add.getName().getAddress().getHostAddress());}
HTML单元:指定IP地址和端口
如果您正在调用HTMLUnit的WebClient,例如:
com.gargoylesoftware.htmlunit.WebClient webClient = new WebClient();
com.gargoylesoftware.htmlunit.html.HtmlPage page =
webClient.getPage("http://mycompany.org/mypage");然后,您可以执行以下任一操作:
com.gargoylesoftware.htmlunit.html.HtmlPage page =
webClient.getPage("http://255.255.255.0:80/mypage");
java.net.URL url = new java.net.URL("http://255.255.255.0:80/mypage");
page = webClient.getPage(url);
com.gargoylesoftware.htmlunit.WebRequest req =
new com.gargoylesoftware.htmlunit.WebRequest(url, com.gargoylesoftware.htmlunit.HttpMethod.GET);
page = webClient.getPage(req);您还可以使用HTMLUnit HttpWebConnection
com.gargoylesoftware.htmlunit.HttpWebConnection conn =
new com.gargoylesoftware.htmlunit.HttpWebConnection(webClient);
java.net.URL url = new java.net.URL("http://255.255.255.0/mypage");
com.gargoylesoftware.htmlunit.WebRequest req =
new com.gargoylesoftware.htmlunit.WebRequest(url, com.gargoylesoftware.htmlunit.HttpMethod.POST);
com.gargoylesoftware.htmlunit.WebRequest resp = conn.getResponse(req);HTTP :为连接指定URL的选项
创建URL以连接到资源(文件、网页等)。您可以使用主机字符串(自动DNS查找)或IP地址指定URL。您可以选择添加端口:
// does not encode URL for you (manually include %20, etc)
java.net.URL url =
new URL("http://255.255.255.20:80/docs/books/tutorial/index.html?name=networking#DOWNLOADING");
java.net.URL url2 = new URL("http", "255.255.255.20", 80,
"/docs/books/tutorial/index.html?name=networking#DOWNLOADING");
java.net.URL url3 = new java.net.URL("https://maps.google.com/maps?q=sydney");
java.net.URL url4 = new java.net.URL("https", "maps.google.com", 80, "/maps?q=sydney");
// does encoding of URL string for you
java.net.URI uri5 = new URI("http", "example.com", "/hello world/", "");
java.net.URL url5 = uri2.toURL();
// Can then pass URL to HTMLUnit, or invoke yourself
// (see Java Network Tutorial for details):
java.net.URLConnection conn = url.openConnection();
java.net.HttpConnection httpConn = (HttpConnection)conn;
// preconnection: call setAllowUserInteraction/setDoInput/setDoOutput/
// setIfModifiedSince/setUseCaches/setRequestProperty
httpConn.connect(); // or getInputStream/getOutputStream/getContent/getHeaderField
// postconnection: call getContentEncoding/getContentLength/getContentType
// getDate/getExpiration/getLastModifed核心Java:指定InetAddress (用于套接字连接)的选项
使用IP地址
// 4 bytes:
byte[] ip4Address = {101,119,11,106};
java.net.InetAddress addressObj = java.net.InetAddress.getByAddress(ip4Address);
// or use ip6Address, 16 bytes (2001:0db8:85a3:0000:0000:8a2e:0370:7334):
// Any hex value above 0x7f must be cast to byte, because byte is signed (-128 to 127)
byte[] ip6Address = {0x20,0x01,0x0d,(byte)0xb8,(byte)0x85,(byte)0xa3,0x00,0x00,0x00,0x00,(byte)0x8a,0x2e,0x03,0x70,0x73,0x34}; 使用URL字符串(DNS查找)
java.net.InetAddress addressObj = java.net.InetAddress.getByName("www.google.com");发布于 2013-06-06 23:01:54
在Java中,您必须将套接字绑定到要使用的NIC的本地IP地址。如果您使用的是0.0.0.0,它将绑定到所有。指定它只使用你想要的那一个。
发布于 2013-06-20 09:28:08
有两种方法可以做到这一点。
InetAddress favourateAddr = InetAddress(byte[] {1,2,3,4});// 1.2.3.4是您要使用的网卡的ip地址
yourHTTPConnObj.setLocalAddress(favourateAddr);
//然后打开http connection yourHTTPConnObj.open();
https://stackoverflow.com/questions/16965388
复制相似问题