Java如何使用HTTPclient访问url获得数据

使用HTTPclient访问url获得数据

最近项目上有个小功能需要调用第三方的http接口取数据,用到了HTTPclient,算是做个笔记吧!

1、使用get方法取得数据

/** 
 * 根据URL试用get方法取得返回的数据 
 * @param url 
 *        URL地址,参数直接挂在URL后面即可 
 * @return 
 */  
public static String getGetDateByUrl(String url){  
    String data = null;  
    //构造HttpClient的实例    
    HttpClient httpClient = new HttpClient();    
    //创建GET方法的实例    
    GetMethod getMethod = new GetMethod(url);   
    //设置头信息:如果不设置User-Agent可能会报405,导致取不到数据  
    getMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:39.0) Gecko/20100101 Firefox/39.0");  
    //使用系统提供的默认的恢复策略    
    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());    
    try{  
        //开始执行getMethod    
        int statusCode = httpClient.executeMethod(getMethod);  
        if (statusCode != HttpStatus.SC_OK) {  
            System.err.println("Method failed:" + getMethod.getStatusLine());  
        }  
        //读取内容  
        byte[] responseBody = getMethod.getResponseBody();  
        //处理内容  
        data = new String(responseBody);  
    }catch (HttpException e){  
        //发生异常,可能是协议不对或者返回的内容有问题  
        System.out.println("Please check your provided http address!");  
        data = "";  
        e.printStackTrace();  
    }catch(IOException e){  
        //发生网络异常  
        data = "";  
        e.printStackTrace();  
    }finally{  
        //释放连接  
        getMethod.releaseConnection();  
    }  
    return data;  
}  

2、使用POST方法取得数据

/** 
 * 根据post方法取得返回数据 
 * @param url 
 *          URL地址 
 * @param array 
 *          需要以post形式提交的参数 
 * @return 
 */  
public static String getPostDateByUrl(String url,Map array){  
    String data = null;  
    //构造HttpClient的实例    
    HttpClient httpClient = new HttpClient();    
    //创建post方法的实例    
    PostMethod postMethod = new PostMethod(url);   
    //设置头信息  
    postMethod.setRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:39.0) Gecko/20100101 Firefox/39.0");  
    postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");  
    //遍历设置要提交的参数  
    Iterator it = array.entrySet().iterator();    
    while (it.hasNext()){  
        Map.Entry entry =(Map.Entry) it.next();    
        String key = entry.getKey();    
        String value = entry.getValue().trim();    
        postMethod.setParameter(key,value);  
    }  
    //使用系统提供的默认的恢复策略    
    postMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());    
    try{  
        //执行postMethod    
        int statusCode = httpClient.executeMethod(postMethod);  
        if (statusCode != HttpStatus.SC_OK) {  
            System.err.println("Method failed:" + postMethod.getStatusLine());  
        }  
        //读取内容  
        byte[] responseBody = postMethod.getResponseBody();  
        //处理内容  
        data = new String(responseBody);  
    }catch (HttpException e){  
        //发生致命的异常,可能是协议不对或者返回的内容有问题  
        System.out.println("Please check your provided http address!");  
        data = "";  
        e.printStackTrace();  
    }catch(IOException e){  
        //发生网络异常  
        data = "";  
        e.printStackTrace();  
    }finally{  
        //释放连接  
        postMethod.releaseConnection();  
    }  
    System.out.println(data);  
    return data;  
}  

使用httpclient后台调用url方式

使用httpclient调用后台的时候接收url类型的参数需要使用UrlDecoder解码,调用的时候需要对参数使用UrlEncoder对参数进行编码,然后调用。

@SuppressWarnings("deprecation")
	@RequestMapping(value = "/wechatSigns", produces = "application/json;charset=utf-8")
	@ResponseBody
	public String wechatSigns(HttpServletRequest request, String p6, String p13) {
		Map ret = new HashMap();
		try {
			System.out.println("*****************************************p6:"+p6);
			URLDecoder.decode(p13);
			System.out.println("*****************************************p13:"+p13);
			String p10 = "{\"p1\":\"1\",\"p2\":\"\",\"p6\":\"" + p6 + "\",\"p13\":\"" + p13 + "\"}";
			p10 = URLEncoder.encode(p10, "utf-8");
			String url = WebserviceUtil.getGetSignatureUrl() + "?p10=" + p10;
			String result = WebConnectionUtil.sendGetRequest(url);
			JSONObject fromObject = JSONObject.fromObject(URLDecoder.decode(result, "utf-8"));
			System.out.println(fromObject);
			String resultCode = JSONObject.fromObject(fromObject.getString("meta")).getString("result");
			if ("0".equals(resultCode)) {
				JSONObject fromObject2 = JSONObject.fromObject(fromObject.get("data"));
				String timestamp = fromObject2.getString("timestamp");
				String appId = fromObject2.getString("appId");
				String nonceStr = fromObject2.getString("nonceStr");
				String signature = fromObject2.getString("signature");
				ret.put("timestamp", timestamp);
				ret.put("appId", appId);
				ret.put("nonceStr", nonceStr);
				ret.put("signature", signature);
				JSONObject jo = JSONObject.fromObject(ret);
				return ResultJsonBean.success(jo.toString());
			} else {
				String resultMsg = JSONObject.fromObject(fromObject.getString("meta")).getString("errMsg");
				return ResultJsonBean.fail(ConnectOauthCodeInfo.REQ_WECHATTOCKEN_CODE, resultMsg, "");
			}
		} catch (Exception e) {
			logger.error(e, e);
			return ResultJsonBean.fail(ConnectOauthCodeInfo.REQ_WECHATREQERROE_CODE,
					ConnectOauthCodeInfo.REQ_WECHATREQERROE_CODE, "");
		}
	}
package com.dcits.djk.core.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
 
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils; 
import net.sf.json.JSONObject;
 
public class WebConnectionUtil {	
	public static String sendPostRequest(String url,Map paramMap,String userId){
		CloseableHttpClient httpclient=null;
		CloseableHttpResponse response=null;		
		try{
			httpclient = HttpClients.createDefault();
			HttpPost httppost = new HttpPost(url);
			List formparams = new ArrayList();
			if(userId != null || "".equals(userId) ){
				formparams.add(new BasicNameValuePair("userId",userId));
			}
			Set keSet=paramMap.entrySet();
			for(Iterator itr=keSet.iterator();itr.hasNext();){
				Map.Entry me=(Map.Entry)itr.next();
				Object key=me.getKey();
				Object valueObj=me.getValue();
				String[] value=new String[1];
				if(valueObj == null){
					value[0] = "";
				}else{
					if(valueObj instanceof String[]){  
			            value=(String[])valueObj;  
			        }else{  
			            value[0]=valueObj.toString();  
			        }
				}
				for(int k=0;k formparams = new ArrayList();
			Set keSet=paramMap.entrySet();
			for(Iterator itr=keSet.iterator();itr.hasNext();){
				Map.Entry me=(Map.Entry)itr.next();
				Object key=me.getKey();
				Object valueObj=me.getValue();
				String[] value=new String[1];
				if(valueObj == null){
					value[0] = "";
				}else{
					if(valueObj instanceof String[]){  
			            value=(String[])valueObj;  
			        }else{  
			            value[0]=valueObj.toString();  
			        }
				}
				for(int k=0;k formparams = new ArrayList();
			Set keSet=paramMap.entrySet();
			for(Iterator itr=keSet.iterator();itr.hasNext();){
				Map.Entry me=(Map.Entry)itr.next();
				Object key=me.getKey();
				Object valueObj=me.getValue();
				String[] value=new String[1];
				if(valueObj == null){
					value[0] = "";
				}else{
					if(valueObj instanceof String[]){  
			            value=(String[])valueObj;  
			        }else{  
			            value[0]=valueObj.toString();  
			        }
				}
				for(int k=0;k 
 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的