首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

java nio开发https代理程序(大家看错哪了?)

2012-01-31 
java nio开发https代理程序(大家看哪里错了?)之前用传统socket开发了一套http代理程序,当然代理https肯定

java nio开发https代理程序(大家看哪里错了?)
之前用传统socket开发了一套http代理程序,当然代理https肯定也是没问题的。现在公司为了提高效率,要求用java nio重写一遍。我用nio写好后发现http能代理,但https不能代理。下面的代码是我从中提取出来的部份代码作为测试程序,大家帮忙看一下错在哪里了,现在是可以代理http,不能代理https,但响应数据也是能收到的,但浏览器显示不出来。

Java code
 
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/*该类用于接收响应的数据,并传送到访问终端*/
class MyThread extends Thread{

private Selector stor;

private SocketChannel sc;

private List <SocketChannel> waitReg = new LinkedList <SocketChannel>();

public MyThread() {
//创建一个信号监视器
try {
stor = Selector.open();
} catch (IOException e) {
e.printStackTrace();
}
}

public void register(SocketChannel dc,SocketChannel sc){
this.sc = sc;
waitReg.add(dc);
stor.wakeup();
}

public void run(){
while(true){
//会阻塞,直到有事件触发,返回事件数目
int count = 0;
try {
count = stor.select();
} catch (IOException e) {
e.printStackTrace();
}
//注册
while(waitReg.size() > 0){
try {
SocketChannel sc = waitReg.get(0);
waitReg.remove(sc);
sc.configureBlocking(false);
sc.register(stor,SelectionKey.OP_READ);
} catch (Exception e) {
e.printStackTrace();
}
}
if(count == 0){
System.out.println("没有发生任何事件");
continue;
}
//遍历所有已触发的事件,进行处理
Iterator it = stor.selectedKeys().iterator();
while(it.hasNext()){
SelectionKey sky = (SelectionKey)it.next();
//将该事件处理完后则删除
it.remove();
if(sky.isReadable()){
System.out.println("接收应用服务器结果");
ByteBuffer bb = ByteBuffer.allocate(10240);
SocketChannel stc =(SocketChannel)sky.channel();
try{
int len = stc.read(bb);
bb.flip();
sc.write(bb);
if(len == -1){
sky.cancel();
}
}catch (IOException e){
sky.cancel();
e.printStackTrace();
continue;
}
}
}
}
}

}

public class Test {

public void run(){
//创建一个信号监视器
Selector stor = null;
try {
stor = Selector.open();
} catch (IOException e) {
e.printStackTrace();
return;
}
//创建一个监听
ServerSocketChannel sc = null;
try {
sc = ServerSocketChannel.open();
//设监听为异步
sc.configureBlocking(false);
} catch (IOException e) {
e.printStackTrace();
return;
}
//绑定监听的端口
try {
sc.socket().bind(new InetSocketAddress(9001));
} catch (IOException e) {
e.printStackTrace();
return;
}
//将监听的OP_ACCEPT类型注册到stor信号监视器
try {
sc.register(stor,SelectionKey.OP_ACCEPT);
} catch (ClosedChannelException e) {
e.printStackTrace();
return;
}

MyThread mt = new MyThread();
mt.start();
//开始循环等待监听事件触发
while(true){
//会阻塞,直到有事件触发,返回事件数目
int count = 0;
try {
count = stor.select();
} catch (IOException e) {
e.printStackTrace();
continue;
}
if(count < 0){


System.out.println("无信号触发");
continue;
}
//遍历所有已触发的事件,进行处理
Iterator it = stor.selectedKeys().iterator();
while(it.hasNext()){
SelectionKey sky = (SelectionKey)it.next();
it.remove();
if(sky.isAcceptable()){
System.out.println("新连接");
ServerSocketChannel serverC =(ServerSocketChannel)sky.channel();
try {
SocketChannel scn = serverC.accept();
scn.configureBlocking(false);
scn.register(stor,SelectionKey.OP_READ);
} catch (IOException e){
e.printStackTrace();
}
}else if(sky.isReadable()){
ByteBuffer bb = ByteBuffer.allocate(10240);
SocketChannel scc = (SocketChannel)sky.channel();
try {
System.out.println("接收参数");
int len = scc.read(bb);
if(len == -1)
sky.cancel();
} catch (IOException e) {
e.printStackTrace();
}
//连接dispacher
SocketChannel dChannel = null;
try {
System.out.println("连接应用服务器");
dChannel = SocketChannel.open();
//dChannel.connect(new InetSocketAddress("192.168.19.45",9001));
dChannel.connect(new InetSocketAddress("127.0.0.1",443));
if(dChannel.finishConnect()){
mt.register(dChannel,scc);
bb.flip();
System.out.println("向应用服务器传参");
dChannel.write(bb);
}else{
System.out.println("连接分配程序失败");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) throws Exception {
new Test().run();
}
}



[解决办法]

热点排行