• 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;
    	}
    
    }
    

      

  • 相关阅读:
    QT::目录/文件
    QT::标题栏/目录/托盘/工具条/状态栏
    QT::布局
    PHP 发送http请求
    php 图片缩放然后合成并保存
    PHP 打开已有图片进行编辑
    一些实用的方法整理(与语言无关)
    PHP 与pdf文档 与条码
    Excel导入遇到的问题An object with the same key already exists in the ObjectStateManager……
    【随笔】Apache降权和禁用PHP危险函数
  • 原文地址:https://www.cnblogs.com/xuxian/p/5976443.html
Copyright © 2020-2023  润新知