1 2 29 30 package com.jcraft.jhttptunnel; 31 32 import java.net.*; 33 import java.io.*; 34 35 public class OutBoundURL extends OutBound{ 36 private InputStream in=null; 37 private OutputStream out=null; 38 private URLConnection con=null; 39 private final byte[] _TUNNEL_DISCONNECT={(byte)0x47}; 40 41 public void connect() throws IOException{ 42 close(); 43 44 String host=getHost(); 45 int port=getPort(); 46 47 URL url=new URL("http://"+host+":"+port+"/index.html?crap=1"); 48 con=url.openConnection(); 49 con.setUseCaches(false); 50 con.setDoOutput(true); 51 con.setRequestProperty("Connection", "none"); 52 out=con.getOutputStream(); 53 sendCount=getContentLength(); 54 } 55 56 public void sendData(byte[] foo, int s, int l, boolean flush) throws IOException{ 57 if(l<=0) return; 59 60 if(con==null){ 61 connect(); 62 } 63 64 if(sendCount<=0){ 65 connect(); 66 } 67 68 int retry=2; 69 while(retry>0){ 70 try{ 71 out.write(foo, s, l); 73 sendCount-=l; 74 if(flush){ 75 if(sendCount>0){ 76 out.write(_TUNNEL_DISCONNECT, 0, 1); 77 } 78 out.flush(); 79 out.close(); 80 out=null; 81 in=con.getInputStream(); 82 close(); 83 84 sendCount=0; 85 return; 86 } 87 return; 88 } 89 catch(SocketException e){ 90 System.out.println("2# "+e+" "+l+" "+flush); 91 throw e; 92 } 94 catch(IOException e){ 95 System.out.println("2# "+e); 97 connect(); 98 } 99 retry--; 100 } 101 } 102 103 public void close() throws IOException{ 104 if(con!=null){ 106 if(out!=null){ try{out.close(); out=null;}catch(IOException e){} } 107 if(in!=null){ 108 try{ 109 116 in.close(); in=null;}catch(IOException e){} } 117 con=null; 118 } 119 } 121 } 122 | Popular Tags |