如何设置socket代理?
采用老的socket(诸塞式),通过代理访问socket server,如下代码可以实现:
public class Client
{
static Socket server;
public static void main(String[] args) throws Exception {
//设置代理.
String proxyHost = "192.168.0.76 ";
String proxyPort = "1080 ";
System.getProperties().put( "socksProxySet ", "true ");
System.getProperties().put( "socksProxyHost ",proxyHost);
System.getProperties().put( "socksProxyPort ",proxyPort);
String host = "192.168.0.76 ";
int port = 5678;
System.out.println( "connetioning: " + host + ": " + port);
server = new Socket(host, port);
PrintWriter out = new PrintWriter(server.getOutputStream());
String str = "test\r\n ";
out.println(str);
out.flush();
System.out.println( "finish send message ");
Thread.sleep(1000);
BufferedReader in = new BufferedReader(new InputStreamReader(server
.getInputStream()));
String resp = in.readLine();
System.out.println(resp);
server.close();
}
}
但是在nio socket中,传统的socket设置代理的方法不能用,如下代码不能通过代理连接server:
String proxyHost = "192.168.0.76 ";
String proxyPort = "1080 ";
System.getProperties().put( "socksProxySet ", "true ");
System.getProperties().put( "socksProxyHost ", proxyHost);
System.getProperties().put( "socksProxyPort ", proxyPort);
try
{
SocketChannel client = SocketChannel.open();
InetSocketAddress isa = new InetSocketAddress(host, port);
client.connect(isa);
client.configureBlocking(false);
}
catch (UnknownHostException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
200分求在nio中如何设置socket代理?
[解决办法]
路过 mark一下
[解决办法]
帮顶
[解决办法]
学习
[解决办法]
方式不同,思想应该一样吧
[解决办法]
UP
[解决办法]
高手快进来呀
------解决方案--------------------
nio无阻塞io,如果要用代理,必须该代理也支持nio
不然的话就没有意义,普通的socks代理不能代理nio连接
[解决办法]
顶起来
期待
[解决办法]
我写的一个程序,用到了代理,希望对你有帮助。
public class SimpleFrame extends JFrame {
private JPanel contendPanel = new JPanel();
private JPanel norPanel = new JPanel();
private JTextArea jtx = new JTextArea();
private JTextField jtf = new JTextField();
private JButton btn = new JButton( "Connect ");
public SimpleFrame() {
contendPanel.setLayout(new BorderLayout());
norPanel.setLayout(new GridLayout());
norPanel.add(jtf);
norPanel.add(btn);
contendPanel.add(jtx);
contendPanel.add(norPanel, BorderLayout.NORTH);
jtf.setText( "http://www.baidu.com ");
this.getContentPane().add(contendPanel);
this.setSize(400, 300);
this.setVisible(true);
}
public void conn() {
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
StringBuffer document = new StringBuffer();
try {
InetAddress addr = InetAddress.getByName( "proxyall.neusoft.com ");
InetSocketAddress sa = new InetSocketAddress(addr.getHostAddress(), 8080);
// 通过代理服务器连接
Proxy proxy = new Proxy(Proxy.Type.HTTP, sa);
Authenticator.setDefault(new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication( "username ", new String( "password ").toCharArray());
}
});
URL url = new URL(jtf.getText());
URLConnection conn = url.openConnection(proxy);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
document.append(line + "\n ");
}
System.out.println(document);
jtx.setText(document.toString());
reader.close();
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
});
}
public static void main(String[] args) {
SimpleFrame simobj = new SimpleFrame();
simobj.conn();
}
}
[解决办法]
学习....顶........
[解决办法]
好像还有个
java.net.ProxySelector
看名字有nio的风格
[解决办法]
不光要顶,还要支持!
希望大家都能上些优质的代码!
[解决办法]
期待
[解决办法]
接分~~~顶一个,东西是好东西,不过帮不了你,对代理不是很了解