用socket实现代理的样子的各种方式:socket实现方式,telnet 实现方式 ,http proxy 实现方式

来源:互联网 发布:金三税个人所得税软件 编辑:程序博客网 时间:2024/04/29 09:34
 1.socket实现方式:

Java代码 复制代码 收藏代码
  1. public class TestMonitor {   
  2.   
  3.     public static void main(String[] args) throws IOException {   
  4.   
  5.         Socket socket = null;   
  6.        // PrintWriter out = null;   
  7.         BufferedReader in = null;   
  8.         PrintStream ops= null;   
  9.         try {   
  10.             socket = new Socket(args[0], 4444);//参数为服务器的ip地址  
  11.             ops=new PrintStream(socket.getOutputStream());   
  12.             ops.println(args[1]); // 参数是发送到服务器上的请求信息  
  13.             ops.flush();      
  14. //            out = new PrintWriter(socket.getOutputStream(), true);  
  15.             in = new BufferedReader(new InputStreamReader(socket.getInputStream()));   
  16.         } catch (UnknownHostException e) {   
  17.             System.err.println("Don't know about host:" + args[0]);   
  18.             System.exit(1);   
  19.         } catch (IOException e) {   
  20.             System.err.println("Couldn't get I/O for the connection to: taranis.");   
  21.             System.exit(1);   
  22.         }   
  23.   
  24.         BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));   
  25.         String fromServer;   
  26.   
  27. //        String fromUser = stdIn.readLine();  
  28. //        System.out.println("Client: " + fromUser);  
  29. //        out.println(fromUser);   
  30.         while ((fromServer = in.readLine()) != null) {   
  31.             System.out.println("Server: " + fromServer);   
  32.             if (fromServer.equals("Bye."))   
  33.                 break;   
  34.             String fromUser = stdIn.readLine();   
  35.             if (fromUser != null) {   
  36.                     System.out.println("Client: " + fromUser);   
  37.                     ops.println(fromUser);   
  38.             }   
  39.         }   
  40.   
  41.         ops.close();   
  42.         in.close();   
  43.         stdIn.close();   
  44.         socket.close();   
  45.     }   
  46. }  


2.telnet 实现方式
这个要加个jar包: commons-net-2.0.jar

Java代码 复制代码 收藏代码
  1. public class TestTelnet {   
  2.   
  3.     /**  
  4.      * @param args  
  5.      */  
  6.     public static void main(String[] args) {   
  7.         TelnetClient tc=new TelnetClient();   
  8.         PrintStream ops= null;   
  9.          BufferedReader in = null;   
  10.         try {   
  11.             tc.connect("172.16.100.87"4444);//连接的服务器和端口  
  12.             System.out.println("test cennection:"+tc.isConnected());           
  13.             ops=new PrintStream(tc.getOutputStream());   
  14.             ops.println("/refreshapps"); // 是发送到服务器上的请求信息  
  15.   
  16.             ops.flush();      
  17.              in = new BufferedReader(new InputStreamReader(tc.getInputStream()));   
  18.           // System.out.println( "AAA"+in.readLine());        
  19.             char[] bs=new char[256];   
  20.             int i=0;   
  21.            
  22.                 while((i=in.read(bs))>-1){   
  23.                     String str=null;   
  24.                     if(i==256){   
  25.                         str=new String(bs);   
  26.                     }else{   
  27.                         char[] bs2=new char[i];   
  28.                         for(int j=0;j<i;j++)   
  29.                         {   
  30.                             bs2[j]=bs[j];   
  31.                         }   
  32.                         str=new String(bs2);   
  33.                     }   
  34.   
  35.                     System.out.println(str);   
  36.                 }   
  37.                    
  38.         } catch (SocketException e) {              
  39.             e.printStackTrace();   
  40.         } catch (IOException e) {          
  41.             e.printStackTrace();   
  42.         }   
  43.            
  44.     }   
  45.        
  46.   
  47. }  


3.http proxy 实现方式



Java代码 复制代码 收藏代码
  1. import java.io.BufferedReader;   
  2. import java.io.IOException;   
  3. import java.io.InputStreamReader;   
  4. import java.net.MalformedURLException;   
  5. import java.net.URL;   
  6. import java.net.URLConnection;   
  7. import java.util.Properties;   
  8.   
  9. public class TestProxy {   
  10.   
  11.     /**  
  12.      * @param args  
  13.      * @throws IOException  
  14.      */  
  15.     public static void main(String[] args) throws IOException {   
  16.   
  17.         // String strUrl="http://blog.csdn.net/cqq/";  
  18.         // URL url = new URL(strUrl);   
  19.         URL url = new URL("http""172.16.100.87"4444"");//所请求服务器的ip地址和端口  
  20.         URLConnection conn = url.openConnection();   
  21.   
  22.         String strProxy = "172.16.10.133";//代理服务器的ip  
  23.         String strPort = "4444";//端口  
  24.         Properties systemProperties = System.getProperties();   
  25.         systemProperties.setProperty("http.proxyHost", strProxy);   
  26.         systemProperties.setProperty("http.proxyPort", strPort);   
  27.   
  28.         BufferedReader rd = new BufferedReader(new InputStreamReader(conn   
  29.                 .getInputStream()));   
  30.         String ss = null;   
  31.         while ((ss = rd.readLine()) != null) {   
  32.             System.out.println(ss);   
  33.         }   
  34.         rd.close();   
  35.   
  36.     }   
  37.   
  38. }  
原创粉丝点击