一、Java访问远程url接口并获取结果
1、原生JavaAPI获取
package com.util;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.Map; /** * <pre> * 功能:httpUrlConnection访问远程接口工具 * 日期:2015年3月17日 上午11:19:21 * </pre> */ public class HttpUrlConnectionUtil { /** * <pre> * 方法体说明:向远程接口发起请求,返回字符串类型结果 * @param url 接口地址 * @param requestMethod 请求方式 * @param params 传递参数 重点:参数值需要用Base64进行转码 * @return String 返回结果 * </pre> */ public static String httpRequestToString(String url, String requestMethod, Map<String, String> params){ String result = null; try { InputStream is = httpRequestToStream(url, requestMethod, params); byte[] b = new byte[is.available()]; is.read(b); result = new String(b); } catch (IOException e) { e.printStackTrace(); } return result; } /** * <pre> * 方法体说明:向远程接口发起请求,返回字节流类型结果 * 作者:itar * 日期:2015年3月17日 上午11:20:25 * @param url 接口地址 * @param requestMethod 请求方式 * @param params 传递参数 重点:参数值需要用Base64进行转码 * @return InputStream 返回结果 * </pre> */ public static InputStream httpRequestToStream(String url, String requestMethod, Map<String, String> params){ InputStream is = null; try { String parameters = ""; boolean hasParams = false; //将参数集合拼接成特定格式,如name=zhangsan&age=24 for(String key : params.keySet()){ String value = URLEncoder.encode(params.get(key), "UTF-8"); parameters += key +"="+ value +"&"; hasParams = true; } if(hasParams){ parameters = parameters.substring(0, parameters.length()-1); } //请求方式是否为get boolean isGet = "get".equalsIgnoreCase(requestMethod); //请求方式是否为post boolean isPost = "post".equalsIgnoreCase(requestMethod); if(isGet){ url += "?"+ parameters; } URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); //请求的参数类型(使用restlet框架时,为了兼容框架,必须设置Content-Type为“”空) conn.setRequestProperty("Content-Type", "application/octet-stream"); //conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //设置连接超时时间 conn.setConnectTimeout(50000); //设置读取返回内容超时时间 conn.setReadTimeout(50000); //设置向HttpURLConnection对象中输出,因为post方式将请求参数放在http正文内,因此需要设置为ture,默认false if(isPost){ conn.setDoOutput(true); } //设置从HttpURLConnection对象读入,默认为true conn.setDoInput(true); //设置是否使用缓存,post方式不能使用缓存 if(isPost){ conn.setUseCaches(false); } //设置请求方式,默认为GET conn.setRequestMethod(requestMethod); //post方式需要将传递的参数输出到conn对象中 if(isPost){ DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(parameters); dos.flush(); dos.close(); } //从HttpURLConnection对象中读取响应的消息 //执行该语句时才正式发起请求 is = conn.getInputStream(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return is; } }
2、利用httpClient访问获取
package com.util;
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.Map; import org.apache.http.HttpResponse; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.util.EntityUtils; /** * <pre> * 功能:httpClient访问远程接口工具类 * 日期:2015年3月17日 上午11:19:21 * </pre> */ @SuppressWarnings("deprecation") public class HttpClientUtil { /** * <pre> * 方法体说明:向远程接口发起请求,返回字符串类型结果 * @param url 接口地址 * @param requestMethod 请求类型 * @param params 传递参数 * @return String 返回结果 * </pre> */ public static String httpRequestToString(String url, String requestMethod, Map<String, String> params, String ...auth){ //接口返回结果 String methodResult = null; try { String parameters = ""; boolean hasParams = false; //将参数集合拼接成特定格式,如name=zhangsan&age=24 for(String key : params.keySet()){ String value = URLEncoder.encode(params.get(key), "UTF-8"); parameters += key +"="+ value +"&"; hasParams = true; } if(hasParams){ parameters = parameters.substring(0, parameters.length()-1); } //是否为GET方式请求 boolean isGet = "get".equalsIgnoreCase(requestMethod); boolean isPost = "post".equalsIgnoreCase(requestMethod); boolean isPut = "put".equalsIgnoreCase(requestMethod); boolean isDelete = "delete".equalsIgnoreCase(requestMethod); //创建HttpClient连接对象 DefaultHttpClient client = new DefaultHttpClient(); HttpRequestBase method = null; if(isGet){ url += "?" + parameters; method = new HttpGet(url); }else if(isPost){ method = new HttpPost(url); HttpPost postMethod = (HttpPost) method; StringEntity entity = new StringEntity(parameters); postMethod.setEntity(entity); }else if(isPut){ method = new HttpPut(url); HttpPut putMethod = (HttpPut) method; StringEntity entity = new StringEntity(parameters); putMethod.setEntity(entity); }else if(isDelete){ url += "?" + parameters; method = new HttpDelete(url); } method.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 6000); //设置参数内容类型 method.addHeader("Content-Type","application/x-www-form-urlencoded"); //httpClient本地上下文 HttpClientContext context = null; if(!(auth==null || auth.length==0)){ String username = auth[0]; String password = auth[1]; UsernamePasswordCredentials credt = new UsernamePasswordCredentials(username,password); //凭据提供器 CredentialsProvider provider = new BasicCredentialsProvider(); //凭据的匹配范围 provider.setCredentials(AuthScope.ANY, credt); context = HttpClientContext.create(); context.setCredentialsProvider(provider); } //访问接口,返回状态码 HttpResponse response = client.execute(method, context); //返回状态码200,则访问接口成功 if(response.getStatusLine().getStatusCode()==200){ methodResult = EntityUtils.toString(response.getEntity()); } client.close(); }catch (UnsupportedEncodingException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } return methodResult; } }