Java } }最后{ 流内关闭(); } } 打破 违约: 抛出新的IllegalArgumentException(“错误”); } } 返回生成器; } } 我不确定这是否是你在问的问题(如果我错了,真的很抱歉),但是。。。您在HttpClient上尝试过类似
} }最后{ 流内关闭(); } } 打破 违约: 抛出新的IllegalArgumentException(“错误”); } } 返回生成器; } }Java } }最后{ 流内关闭(); } } 打破 违约: 抛出新的IllegalArgumentException(“错误”); } } 返回生成器; } } 我不确定这是否是你在问的问题(如果我错了,真的很抱歉),但是。。。您在HttpClient上尝试过类似,java,linux,ip,httpurlconnection,apache-commons-httpclient,Java,Linux,Ip,Httpurlconnection,Apache Commons Httpclient,} }最后{ 流内关闭(); } } 打破 违约: 抛出新的IllegalArgumentException(“错误”); } } 返回生成器; } } 我不确定这是否是你在问的问题(如果我错了,真的很抱歉),但是。。。您在HttpClient上尝试过类似的方法吗字节ip[]=新字节[]{(字节)192,(字节)168,1105};client.getParams().setParameter(ConnRouteParams.LOCAL_地址,InetAddress.getByAddress(i
我不确定这是否是你在问的问题(如果我错了,真的很抱歉),但是。。。您在HttpClient上尝试过类似的方法吗<代码>字节ip[]=新字节[]{(字节)192,(字节)168,1105};client.getParams().setParameter(ConnRouteParams.LOCAL_地址,InetAddress.getByAddress(ip))你好,谢谢你的建议。这是我在使用HttpClient库时看到的方式。你对java.net有什么想法吗?对不起,对java.net.Hi没有想法。我刚刚实现了这个方法,但它不起作用。无论如何,代理类型应该是HTTP,连接类型应该是HttpURLConnection。但是,代理配置不起作用。当我尝试在不使用代理的情况下建立正常连接时,一切正常。因此,问题仍然悬而未决!意味着关于您试图指定特定地址的问题是??然后将该地址设置为默认地址,然后再次尝试运行na。。试一下,即使我对这件事感到困惑,它也不起作用。我也在做同样的工作。谢谢你的建议。你能给我提供一些基本的代码示例吗,还是只是一个尝试?@Krishna没错,它使用两个不同的本地接口(NIC)发送相同的HTTP请求。在我的示例中,eth0可以是192.168.100.32,eth1可以是192.168.100.33。使您能够通过您选择的本地地址(NIC)而不是系统选择的地址发送数据。谢谢。我知道Apache库可以很好地处理这一问题,但我正试图找到一种与本机java.net兼容的解决方案。@m121212欢迎您。我将此代码留给您,以防您最终放弃使用java.net:因为它不是字节ip1[],而是字节ip[/code>,对吗?谢谢你的解决方案!在只使用本机java.net的社区中仍然找不到好的答案@例如,字节ip1[]也是正确的。见:
public class test {
public static void main(String[] args) {
final String authUser = "admin";
final String authPassword = "password";
Authenticator.setDefault(
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
authUser, authPassword.toCharArray());
}
}
);
System.setProperty("http.proxyUser", authUser);
System.setProperty("http.proxyPassword", authPassword);
try {
Properties systemProperties = System.getProperties();
URL url = new URL("yourURL");
systemProperties.setProperty("http.proxyHost","localhost");
systemProperties.setProperty("http.proxyPort", "8080");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int status = connection.getResponseCode();
System.out.println(status);
} catch (IOException e) {
System.out.println("connection problems");
}
}
}
URL url = new URL(yourUrlHere);
Proxy proxy = new Proxy(Proxy.Type.DIRECT,
new InetSocketAddress(
InetAddress.getByAddress(
new byte[]{your, ip, interface, here}), yourTcpPortHere));
URLConnection conn = url.openConnection(proxy);
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:18:a5:97 brd ff:ff:ff:ff:ff:ff
inet 100.10.52.15/24 brd 10.0.2.255 scope global eth1
inet 100.10.52.16/24 brd 10.0.2.255 scope global secondary eth1
inet6 fe80::a00:27ff:fe18:a597/64 scope link
valid_lft forever preferred_lft forever
3: eth2: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:11:1f:0c brd ff:ff:ff:ff:ff:ff
inet 158.17.47.19/24 brd 172.17.37.255 scope global eth2
inet6 fe80::a00:27ff:fe11:1f0c/64 scope link
valid_lft forever preferred_lft forever
4: eth3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:cf:96:2d brd ff:ff:ff:ff:ff:ff
inet6 fe80::a00:27ff:fecf:962d/64 scope link
valid_lft forever preferred_lft forever
ip addr del 100.10.25.15/24 dev eth0
ip addr del 100.10.25.16/24 dev eth0
ip addr add 100.10.25.16/24 dev eth0
ip addr add 100.10.25.15/24 dev eth0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class HttpClientExample {
public void gogo() throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
// Local interface1:
byte ip1[] = new byte[] { (byte)192, (byte)168, (byte)100, (byte)32 };
// Local interface2:
byte ip2[] = new byte[] { (byte)192, (byte)168, (byte)100, (byte)33 };
RequestConfig requestConfig = RequestConfig.custom().setLocalAddress(InetAddress.getByAddress(ip1)).build();
try {
HttpGet httpget = new HttpGet("http://server.com");
httpget.setConfig(requestConfig);
System.out.println("executing request" + httpget.getRequestLine());
StringBuilder response = httpclient.execute(httpget,handler);
System.out.println(response.toString());
requestConfig = RequestConfig.custom().setLocalAddress(InetAddress.getByAddress(ip2)).build();
httpget = new HttpGet("http://server.com");
httpget.setConfig(requestConfig);
System.out.println("executing request" + httpget.getRequestLine());
response = httpclient.execute(httpget,handler);
System.out.println(response.toString());
} finally {
httpclient.close();
}
}
private final ResponseHandler<StringBuilder> handler = new ResponseHandler<StringBuilder>() {
@Override
public StringBuilder handleResponse(final HttpResponse response)
throws ClientProtocolException, IOException {
return sortResponse(response);
}
};
private StringBuilder sortResponse(final HttpResponse httpResponse) throws IOException {
StringBuilder builder = null;
if (httpResponse != null) {
switch (httpResponse.getStatusLine().getStatusCode()) {
case HttpStatus.SC_OK:
final HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
final InputStreamReader instream = new InputStreamReader(entity.getContent());
try {
final BufferedReader reader = new BufferedReader(instream);
builder = new StringBuilder();
String currentLine = null;
currentLine = reader.readLine();
while (currentLine != null) {
builder.append(currentLine).append("\n");
currentLine = reader.readLine();
}
} finally {
instream.close();
}
}
break;
default:
throw new IllegalArgumentException("Error.");
}
}
return builder;
}
}