afunms

          My Software,My Dream—Forge a more perfect NMS product.

          java http proxy

          package afu.proxy;

          import java.net.*;
          import java.io.*;

          import org.apache.commons.httpclient.*;
          import org.apache.commons.httpclient.methods.*;

          public class HttpProxy extends Thread
          {
              
          public static int threadNumber = 0;
              
          private static MultiThreadedHttpConnectionManager 
                                  connectionManager 
          = new MultiThreadedHttpConnectionManager();
              
          private Socket proxyServer;
              
              
          public HttpProxy(Socket ps)
              
          {
                  threadNumber 
          ++;
                  proxyServer 
          = ps;
              }

              
              
          public void run()
              
          {                
                  DataInputStream proxyIn 
          = null;
                  
          try
                  
          {
                      String clientIP 
          = proxyServer.getInetAddress().toString().substring(1);
                      System.out.println(
          "client ip is " + clientIP);
                      
                      
          /**
                       * visit outside websites through proxy server
                       
          */

                      proxyIn 
          = new DataInputStream(proxyServer.getInputStream());    
                      String head 
          = proxyIn.readLine();//read the first row
                      String method = getMethod(head);
                      String requestUrl 
          = getUrl(method.length() + 1 ,head);
                      proxy(method,requestUrl);
                      
                      threadNumber 
          --;
                  }

                  
          catch(Exception e)
                  
          {
                      
          //e.printStackTrace();
                  }

                  
          finally
                  
          {
                      
          try
                      
          {
                          
          if(proxyIn!=null)
                             proxyIn.close();
                          proxyIn 
          = null;
                      }

                      
          catch(Exception e)
                      
          {
                      }

                  }
                  
              }

              
              
          private void proxy(final String methodName,final String requestUrl)
              
          {        
                  DataOutputStream proxyOut 
          = null;
                  InputStream webIn 
          = null;
                  HttpMethod method 
          = null;
                  
          try
                  
          {
                      HttpClient client 
          = new HttpClient(connectionManager);
                      
          if("POST".equals(methodName))
                         method 
          = new PostMethod(requestUrl);
                      
          else
                         method 
          = new GetMethod(requestUrl);            
                      client.executeMethod(method);                                               
                      
                      webIn 
          = new DataInputStream(new BufferedInputStream(method.getResponseBodyAsStream(),10240));
                      proxyOut 
          = new DataOutputStream(proxyServer.getOutputStream());
                      
          int num = webIn.read();
                      
          while(num!=-1)
                      
          {
                          proxyOut.write(num);
                          num 
          = webIn.read();
                      }

                      proxyOut.flush();
                      client 
          = null;
                  }
          //try
                  catch(Exception e)
                  
          {
                      
          //e.printStackTrace();
                  }

                  
          finally
                  
          {
                      
          try
                      
          {
                          proxyOut.close();
                          webIn.close();
                          
          if(method!=null)
                             method.releaseConnection();
                          proxyOut 
          = null;
                          webIn 
          = null;
                          proxyServer.close();
                      }

                      
          catch(Exception e)
                      
          {
                      }
                          
                  }
                  
              }


              
          private String getMethod(final String head)
              
          {
                  
          if(head.indexOf("POST")!=-1)
                     
          return "POST";    
                  
          return "GET";
              }
              
              
              
          private String getUrl(final int startPos,final String head)
              
          {
                  
          int loc = head.indexOf("HTTP/1.");
                  
          return head.substring(startPos,loc - 1);
              }
              
          }

          package afu.proxy;

          import java.net.ServerSocket;
          import java.net.Socket;

          public class StartProxy
          {    
              
          public void doProxy() 
              
          {        
                  
          int port = 808;
                  System.out.println(
          "在端口" + port +"啟動代理服務");
                  
          try
                  
          {
                      ServerSocket ss 
          = new ServerSocket(port);
                      
          while(true)
                      
          {
                          
          if(HttpProxy.threadNumber > 8)
                             
          try 
                             
          {
                                 Thread.sleep(
          500); 
                             }
           
                             
          catch(Exception ignored ) 
                             
          {}
                          
          else
                          
          {
                              Socket socket 
          = ss.accept();               
                              HttpProxy proxy 
          = new HttpProxy(socket);           
                              proxy.start();
                          }

                      }

                  }
              
                  
          catch(Exception e)
                  
          {
                      e.printStackTrace();
                  }

              }

          }

          網上找了很多開源的java  http代理程序,都不好用,最后還是自己寫了一個。

          posted on 2008-08-27 15:41 afunms 閱讀(2460) 評論(1)  編輯  收藏

          Feedback

          # re: java http proxy 2010-03-23 08:31 Ganky

          你真牛!感覺比我以前下載的好  回復  更多評論   



          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          My Links

          News

          留言簿(18)

          隨筆檔案

          相冊

          搜索

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 田东县| 孝昌县| 大冶市| 普兰县| 湘西| 天津市| 瓦房店市| 绿春县| 宁河县| 沁水县| 修武县| 娄烦县| 井研县| 塘沽区| 太湖县| 南平市| 津南区| 河间市| 土默特左旗| 平湖市| 衡东县| 揭阳市| 昌都县| 南丹县| 洪泽县| 洛隆县| 磐安县| 安西县| 方城县| 准格尔旗| 井研县| 和林格尔县| 广元市| 建瓯市| 饶平县| 察隅县| 祁连县| 伊宁县| 望都县| 洛阳市| 福泉市|