HttpsSendData.java 6.74 KB
Newer Older
peterchu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
package com.yutu.base.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpClientParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;


public class HttpsSendData {   
    /**
     * @param url 请求地址
     * @param paramsMap 将参数的名作为键
     * @return
     */
	public static Logger logger= Logger.getLogger(HttpsSendData.class.getName());
	
    public static String send(String url, Map<String, String> paramsMap) {
        String result = null;  
        PostMethod postMethod = null;
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter(  
                HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        postMethod = new PostMethod(url);

        if (paramsMap != null && paramsMap.size() > 0) {  
            NameValuePair[] datas = new NameValuePair[paramsMap.size()];
            int index = 0;  
            for (String key : paramsMap.keySet()) {  
                datas[index++] = new NameValuePair(key,paramsMap.get(key));
            }  
            postMethod.setRequestBody(datas);  
        }  
        HttpClientParams httparams = new HttpClientParams();
        httparams.setSoTimeout(300000);
        postMethod.setParams(httparams);
        try {  
            int statusCode = httpClient.executeMethod(postMethod);  
            if (statusCode == HttpStatus.SC_OK) {
            	//result = postMethod.getResponseBodyAsString();//原来的方式
            	InputStream is = postMethod.getResponseBodyAsStream();
                Scanner scanner = new Scanner(is, "UTF-8");
                result=scanner.useDelimiter("\\A").next();
                
            } else {  
            	System.out.println("请求状态:"+statusCode);
            }  
        } catch (Exception e) {
        	e.printStackTrace();
        }finally {  
            if (postMethod != null) {
            	//释放链接
                postMethod.releaseConnection();  
            }
            if(url.contains("insert.do") || url.contains("update.do") || url.contains("delete")){
            	logger.info("url:"+url);
            	logger.info("paramsMap:"+paramsMap);
            	logger.info("result:"+ JSONObject.toJSONString(result));
            }
            if(url.contains("notify") || url.contains("assistant")){
            	logger.info("url:"+url);
            	logger.info("paramsMap:"+paramsMap);
            	logger.info("result:"+ JSONObject.toJSONString(result));
            }
            if(url.contains("ia")){
                logger.info("url:"+url);
                logger.info("paramsMap:"+paramsMap);
                logger.info("result:"+ JSONObject.toJSONString(result));
            }
        }
        return result;  
    }
    
    
    
	public static String getPost(String url, String body) {
    	
    	//System.out.println("准备开始发送请求:"+System.currentTimeMillis());
        String result = null;  
        PostMethod postMethod = new PostMethod(url);
        if(body!=null && !"".equals(body)){
        	StringRequestEntity requestEntity;
			try {
				requestEntity = new StringRequestEntity(body,"application/json","UTF-8");
				postMethod.setRequestEntity(requestEntity);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        HttpClientParams httparams = new HttpClientParams();
        httparams.setSoTimeout(180000);
        postMethod.setParams(httparams);  
        postMethod.setRequestHeader("Content-Type","application/json;charset=UTF-8");
        
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter(  
                HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        try {  
            int statusCode = httpClient.executeMethod(postMethod);  
            if (statusCode == HttpStatus.SC_OK) {
            	//result = postMethod.getResponseBodyAsString();//原来的方式
            	InputStream is = postMethod.getResponseBodyAsStream();
                Scanner scanner = new Scanner(is, "UTF-8");
                result=scanner.useDelimiter("\\A").next();
            } else {  
            	System.out.println("请求状态:"+statusCode);
            }  
        } catch (Exception e) {
        	e.printStackTrace();
        }finally {  
            if (postMethod != null) {
            	//释放链接
                postMethod.releaseConnection();  
            }  
        }
        //System.out.println("收到响应:"+System.currentTimeMillis());
        return result;  
    	
		
	}
	
	
	public static String httpPost(String url, String body) {
    	
    	//System.out.println("准备开始发送请求:"+System.currentTimeMillis());
        String result = null;  
        PostMethod postMethod = new PostMethod(url);
        if(body!=null && !"".equals(body)){
        	StringRequestEntity requestEntity;
			try {
				requestEntity = new StringRequestEntity(body,"application/json","UTF-8");
				postMethod.setRequestEntity(requestEntity);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        
        HttpClientParams httparams = new HttpClientParams();
        httparams.setSoTimeout(180000);
        postMethod.setParams(httparams);  
        postMethod.setRequestHeader("Content-Type","application/json;charset=UTF-8");
        
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter(  
                HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
        try {  
            int statusCode = httpClient.executeMethod(postMethod);  
            if (statusCode == HttpStatus.SC_OK) {
            	//result = postMethod.getResponseBodyAsString();//原来的方式
            	InputStream is = postMethod.getResponseBodyAsStream();
                Scanner scanner = new Scanner(is, "UTF-8");
                result=scanner.useDelimiter("\\A").next();
            } else {  
            	System.out.println("请求状态:"+statusCode);
            }  
        } catch (Exception e) {
        	e.printStackTrace();
        }finally {  
            if (postMethod != null) {
            	//释放链接
                postMethod.releaseConnection();  
            }  
        }
        //System.out.println("收到响应:"+System.currentTimeMillis());
        return result;
	}



	

}