• HttpRequestProxy


    /************************************************************************
    日  期:		
    作  者:		
    版  本:      
    描  述:	    
    历  史:      
    ************************************************************************/
    package jetsennet.net;
    
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.io.StringWriter;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.HashMap;
    
    /**
     * http请求
     * @author lixiaomin
     *
     */
    public class HttpRequestProxy
    {
    
    	/**
    	 * 获取http内容
    	 * @param requestUrl
    	 * @return
    	 * @throws Exception
    	 */
    	public static String get(String requestUrl) throws Exception
    	{
    		return get(requestUrl,"utf-8");
    	}
    	
    	public static String get(String requestUrl,String charset) throws Exception
    	{
    		URL url = new URL(requestUrl);
    		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    		StringWriter sw = new StringWriter();
    		
    		try
    		{
    			InputStream inStream = connection.getInputStream();
    			InputStreamReader inr = new InputStreamReader(inStream, java.nio.charset.Charset.forName(charset));
    			char[] buf = new char[8096];
    			int size = 0;
    			
    			try{
    				while ((size = inr.read(buf)) != -1)
    					sw.write(buf, 0, size);
    			}
    			finally
    			{
    				sw.close();
    				inr.close();
    				inStream.close();
    			}
    		}		
    		finally
    		{
    			connection.disconnect();
    		}
    		return sw.toString();
    	}	
    	
    	/**
    	 * 以POST方式发送内容并获取数据
    	 * @param requestUrl
    	 * @param content
    	 * @return
    	 * @throws Exception
    	 */
    	public static String send(String requestUrl,String content)throws Exception
    	{
    		return send(requestUrl,content,null,"utf-8");
    	}
    	
    	public static String send(String requestUrl,String content,
    			HashMap<String,String> requestProperties,String charset)throws Exception
    	{
    		URL url = new URL(requestUrl);
    		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    		connection.setDoOutput(true);
    		connection.setDoInput(true);
    		connection.setRequestMethod("POST");
    		
    		if(requestProperties!=null)
    		{
    			for(String key:requestProperties.keySet())
    			{
    				String keyValue = requestProperties.get(key);
    				connection.setRequestProperty(key,keyValue);
    				
    				if("ConnectTimeOut".equalsIgnoreCase(key))
    				{
    					connection.setConnectTimeout(Integer.parseInt(keyValue));
    				}
    				else if("ReadTimeOut".equalsIgnoreCase(key))
    				{
    					connection.setReadTimeout(Integer.parseInt(keyValue));
    				}
    			}
    		}
    
    		OutputStream outStream = connection.getOutputStream();
    		OutputStreamWriter osw = new OutputStreamWriter(outStream, java.nio.charset.Charset.forName(charset));		
    		String response = null;
    		
    		try {
    			try {
    				osw.write(content);
    			}
    			finally {
    				osw.close();
    				outStream.close();
    			}
    
    			InputStream inStream = null;
    			InputStreamReader inStreamReader = null;
    			
    			try {
    				inStream = connection.getInputStream();
    			}
    			catch (Exception exception) {
    				inStream = connection.getErrorStream();
    			}
    			
    			inStreamReader = new InputStreamReader(inStream, java.nio.charset.Charset.forName(charset));
    			StringWriter sw = new StringWriter();
    			char[] buf = new char[8096];
    			int size = 0;
    			
    			try
    			{
    				while ((size = inStreamReader.read(buf)) != -1)
    					sw.write(buf, 0, size);
    	
    				response = sw.toString();
    			}
    			finally
    			{
    				sw.close();
    				inStreamReader.close();
    				inStream.close();
    			}
    		}
    		finally {
    			connection.disconnect();
    		}
    		
    		return response;
    	}
    }
    

      

  • 相关阅读:
    结构体 和 类 的区别
    运算符重载
    迭代器
    HttpClient 同时上传多个文件及参数, 同时利用 Web Api 接收
    JS 上传图片时实现预览
    web api 如何通过接收文件流的方式,接收客户端及前端上传的文件
    同时上传参数及图片到 Web Api
    jsp jstl标签库核心标签
    jsp jstl的使用
    jsp 简单标签开发
  • 原文地址:https://www.cnblogs.com/xuxian/p/5976495.html
Copyright © 2020-2023  润新知