• HttpProxy 1


    package jetsennet.ia.business;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.NameValuePair;
    import org.apache.commons.httpclient.URIException;
    import org.apache.commons.httpclient.methods.DeleteMethod;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.methods.PostMethod;
    import org.apache.commons.httpclient.methods.PutMethod;
    import org.apache.commons.httpclient.params.HttpClientParams;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    
    /************************************************************************
    日  期:	   2013-10-14
    作  者:		周波
    版  本:     smg10
    描  述:	    智能分析http访问代理
    历  史:      
    ************************************************************************/
    public class IAHttpProxy
    {
    	private final static String  USERNAME = "jetsenadmin";//TRS用户名
    	private final static String  PASSWORD = "jetsen@admin";//TRS密码
    	private final static int  timeOut = 30000; //毫秒
    	private final static HttpClient httpClient = new HttpClient();
    	
    	/**
    	 * 发送GET请求
    	 * @param url
    	 * @return
    	 */
    	public static String doGetMethod(String url, List<NameValuePair> pList)throws Exception
    	{
    		String result = "";
    		
    		GetMethod method = new GetMethod(url);
    		method.addRequestHeader("Content-Type", "text/html");//设定请求
    		HttpMethodParams httpParams = new HttpClientParams(); 
    		httpParams.setSoTimeout(timeOut);		
    		method.setParams(httpParams);
    		List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
    		paramsList.add(new NameValuePair("username",USERNAME));
    		paramsList.add(new NameValuePair("password",PASSWORD));
    		if(pList != null)
    		{
    			paramsList.addAll(pList);
    		}
    		NameValuePair[] params = new NameValuePair[paramsList.size()];
    		paramsList.toArray(params);	
    						
    		try
    		{			
    			method.setQueryString(params);
    			int status = httpClient.executeMethod(method);
    			result = method.getResponseBodyAsString();		
    		}
    		catch (URIException e) {
    			// TODO: handle exception
    			throw new Exception("[执行HTTP Get请求时,发生异常,可能超时。]");
    		}
    		catch (IOException e)
    		{
    			throw new Exception("[执行HTTP Get请求时,发生异常,可能超时。]");
    		}
    		finally
    		{
    			method.releaseConnection();			
    		}
    		
    		method.releaseConnection();
    		return result;
    	}
    	
    	/**
    	 * 发送POST请求
    	 * @param url
    	 * @return
    	 */
    	public static String doPostMethod(String url, List<NameValuePair> pList)throws Exception
    	{
    		String result = "";		
    		PostMethod method = new PostMethod(url);		
    		
    		method.addRequestHeader("Content-Type", "text/html");//设定请求
    		HttpMethodParams httpParams = new HttpClientParams(); 
    		httpParams.setSoTimeout(timeOut);		
    		method.setParams(httpParams);
    		List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
    		paramsList.add(new NameValuePair("username",USERNAME));
    		paramsList.add(new NameValuePair("password",PASSWORD));
    		if(pList != null)
    		{
    			paramsList.addAll(pList);
    		}
    		NameValuePair[] params = new NameValuePair[paramsList.size()];
    		paramsList.toArray(params);		
    		
    		
    		try
    		{
    			method.setQueryString(params);
    			
    			int status = httpClient.executeMethod(method);	
    			System.out.println("请求结果状态:"+status);
    			result = method.getResponseBodyAsString();
    		}
    		catch (URIException e) {
    			// TODO: handle exception
    			throw new Exception("[执行HTTP POST请求时,发生异常,可能超时。]");
    		}
    		catch (IOException e)
    		{
    			throw new Exception("[执行HTTP POST请求时,发生异常,可能超时。]");
    		}
    		finally
    		{
    			method.releaseConnection();			
    		}
    		
    		
    		return result;
    	}
    	
    	/**
    	 * 发送PUT请求
    	 * @param url
    	 * @return
    	 */
    	public static String doPutMethod(String url, List<NameValuePair> pList)throws Exception
    	{
    		String result = "";		
    		PutMethod method = new PutMethod(url);		
    	
    		method.addRequestHeader("Content-Type", "text/html");//设定请求
    		HttpMethodParams httpParams = new HttpClientParams(); 
    		httpParams.setSoTimeout(timeOut);		
    		method.setParams(httpParams);
    		
    		List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
    		paramsList.add(new NameValuePair("username",USERNAME));
    		paramsList.add(new NameValuePair("password",PASSWORD));
    		if(pList != null)
    		{
    			paramsList.addAll(pList);
    		}
    		NameValuePair[] params = new NameValuePair[paramsList.size()];
    		paramsList.toArray(params);		
    				
    		try
    		{
    			method.setQueryString(params);			
    			int status = httpClient.executeMethod(method);				
    			result = method.getResponseBodyAsString();
    			
    		}
    		catch (URIException e) {
    			// TODO: handle exception
    			throw new Exception("[执行HTTP Put请求时,发生异常,可能超时。]");
    		}
    		catch (IOException e)
    		{
    			throw new Exception("[执行HTTP Put请求时,发生异常,可能超时。]");
    		}
    		finally
    		{
    			method.releaseConnection();			
    		}
    				
    		return result;
    	}
    	
    	/**
    	 * 发送Delete请求
    	 * @param url
    	 * @return
    	 */
    	public static String doDeleteMethod(String url, List<NameValuePair> pList)throws Exception
    	{
    		String result = "";
    		
    		DeleteMethod method = new DeleteMethod(url);		
    	
    		method.addRequestHeader("Content-Type", "text/html");//设定请求
    		HttpMethodParams httpParams = new HttpClientParams(); 
    		httpParams.setSoTimeout(timeOut);		
    		method.setParams(httpParams);
    		
    		List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
    		paramsList.add(new NameValuePair("username",USERNAME));
    		paramsList.add(new NameValuePair("password",PASSWORD));
    		if(pList != null)
    		{
    			paramsList.addAll(pList);
    		}
    		NameValuePair[] params = new NameValuePair[paramsList.size()];
    		paramsList.toArray(params);		
    		
    		
    		try
    		{
    			method.setQueryString(params);
    			
    			int status = httpClient.executeMethod(method);			
    			result = method.getResponseBodyAsString();
    			
    		}
    		catch (URIException e) {
    			// TODO: handle exception
    			throw new Exception("[执行HTTP delete请求时,发生异常,可能超时。]");
    		}
    		catch (IOException e)
    		{
    			throw new Exception("[执行HTTP delete请求时,发生异常,可能超时。]");
    		}
    		finally
    		{
    			method.releaseConnection();			
    		}		
    		
    		return result;
    	}
    
    }
    

      

  • 相关阅读:
    MongoDB性能分析
    MongoDB复制
    redis键管理
    MySQL集群架构-DRBD+headbeat +lvs+keepalived
    Spark-Core RDD转换算子-双Value型交互
    Spark-Core RDD转换算子-Value型
    Spark-Core RDD的创建
    Spark-Core RDD概述
    数仓理论
    flume 进阶
  • 原文地址:https://www.cnblogs.com/xuxian/p/5976443.html
Copyright © 2020-2023  润新知