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 阅读(2454) 评论(1)  编辑  收藏

Feedback

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

你真牛!感觉比我以前下载的好  回复  更多评论   



只有注册用户登录后才能发表评论。


网站导航:
 

My Links

News

留言簿(18)

随笔档案

相册

搜索

最新评论

阅读排行榜