后台URL调用,支持跨域,直接使用

package com.ky.ft.util;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import javax.net.ssl.SSLContext;


import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;


public class HttpUtil {




public static String get(String url){
String result=null;
    try {
    CloseableHttpClient client = createSSLClientDefault();
    HttpGet get = new HttpGet(url);
CloseableHttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity());
                return result;  
            } 
} catch (IOException e) {
e.printStackTrace();
}
return result==null?"":result;
}

public static String post(String url, Map<String, Object> map){
String result=null;
try {
CloseableHttpClient client = createSSLClientDefault();
HttpPost post=new HttpPost(url);
post.setHeader("accept", "*/*");
post.setHeader("connection", "Keep-Alive");
post.setHeader("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

List<NameValuePair> nvps = new ArrayList<NameValuePair>();   
            for (String key:map.keySet()) {
                nvps.add(new BasicNameValuePair(key, String.valueOf(map.get(key))));  
            }  
            post.setEntity(new UrlEncodedFormEntity(nvps,"UTF-8"));  
CloseableHttpResponse response = client.execute(post);
StatusLine status = response.getStatusLine();  
            int state = status.getStatusCode();  
            if (state == HttpStatus.SC_OK) {  
                HttpEntity responseEntity = response.getEntity();  
                result = EntityUtils.toString(responseEntity);  
            }  
            else{  
                 System.out.println("请求返回:"+state+"("+url+")");  
            }
} catch (IOException e) {
e.printStackTrace();
}  
return result==null?"":result;
}

/**
* 用来绕过证书检查
* @return
*/
public static CloseableHttpClient createSSLClientDefault(){  
        try {  
             SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {  
                 //信任所有证书  
                 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {  
                     return true;  
                 }  
             }).build();  
             SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(sslContext,SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
             return HttpClients.custom().setSSLSocketFactory(sslFactory).build();  
         } catch (Exception e) {  
             e.printStackTrace();
         }  
         return  HttpClients.createDefault();  
    }

/**
     * 将map编成http类型的参数返回(key1=value1&key2=value2)
     * @param map
     * @return
     */
    public static String param(Map<String, Object> map){
    return param(map, null);
    }
    
    /**
     * 将map编成http类型的参数返回(key1=value1&key2=value2),有charset则编对应的url编码字符串
     * @param map
     * @param charset
     * @return
     */
    public static String param(Map<String, Object> map,String charset){
    StringBuffer sb=new StringBuffer();
    if(map!=null&&!map.isEmpty()){
    for(String key:map.keySet()){
    sb.append(key).append("=").append(map.get(key)).append("&");
    }
    sb.deleteCharAt(sb.length()-1);
    if(charset!=null){
    try {
    return URLEncoder.encode(sb.toString(), charset);
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    }
    }
    }
    return sb.toString();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值