JAVA访问url POST请求
2015-06-13 12:25
399 查看
分享一把~~~
返回页面
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.security.KeyStore; import java.util.ArrayList; import java.util.List; import java.util.Map; import javax.net.ssl.SSLContext; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; 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.conn.ssl.SSLContexts; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; public class WebClientUtils { public static String getGetText(CloseableHttpClient httpclient,String url){ if(httpclient==null){ httpclient = HttpClientBuilder.create().build(); } HttpGet httpget = new HttpGet(url); String responseBody = ""; try { HttpResponse response = httpclient.execute(httpget); int statusCode=response.getStatusLine().getStatusCode(); if(statusCode>=300&&statusCode<400){ //跳转 responseBody = getGetText(httpclient,response.getFirstHeader("Location").getValue()); }else{ InputStream in=response.getEntity().getContent(); responseBody =in2Text(in); } } catch (Exception e) { e.printStackTrace(); responseBody = ""; } finally { if(httpget!=null){ httpget.abort(); } } return responseBody; } /** * 参数格式 x=x , y=y * @param httpclient * @param url * @param params * @return */ public static String getPostText(CloseableHttpClient httpclient,String url,String ... params){ HttpPost httpPost = new HttpPost(url); String text=""; if(httpclient==null){ httpclient= HttpClientBuilder.create().build(); } List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for(String param:params){ String temp[]=param.split("="); nvps.add(new BasicNameValuePair(temp[0], temp[1])); } try { httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); HttpResponse response = httpclient.execute(httpPost); text= in2Text(response.getEntity().getContent()); } catch (Exception e) { e.printStackTrace(); return ""; } finally { httpPost.abort(); } return text; } public static String getPostText(CloseableHttpClient httpclient,String url,Map<String,String> params){ HttpPost httpPost = new HttpPost(url); String text=""; if(httpclient==null){ httpclient=HttpClientBuilder.create().build(); } List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for(String key:params.keySet()){ nvps.add(new BasicNameValuePair(key, params.get(key))); } try { httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8")); HttpResponse response = httpclient.execute(httpPost); text= in2Text(response.getEntity().getContent()); } catch (Exception e) { e.printStackTrace(); return ""; } finally { httpPost.abort(); } return text; } public static String getPostText(CloseableHttpClient httpclient,String url,String param){ HttpPost httpPost = new HttpPost(url); String text=""; if(httpclient==null){ httpclient=HttpClientBuilder.create().build(); } try { StringEntity entity = new StringEntity(param,"UTF-8"); //entity.setContentType("application/x-www-form-urlencoded"); httpPost.addHeader("charset", "UTF-8"); httpPost.setEntity(entity); HttpResponse response = httpclient.execute(httpPost); text= in2Text(response.getEntity().getContent()); } catch (Exception e) { e.printStackTrace(); return ""; } finally { httpPost.abort(); } return text; } public static String getP12PostText(String url,String param,String cerPath ,String password){ /* try{ KeyStore keyStore = KeyStore.getInstance("PKCS12"); InputStream ksIn = new FileInputStream(cerPath); try{ keyStore.load(ksIn, password.toCharArray()); }catch(Exception e){ }finally{ ksIn.close(); } SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, password, keyStore); Scheme sch = new Scheme("https", 443, socketFactory); httpclient.getConnectionManager().getSchemeRegistry().register(sch); return getPostText(httpclient, url, param); }catch(Exception e){ e.printStackTrace(); }*/ try{ KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File(cerPath)); try { keyStore.load(instream, password.toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom() .loadKeyMaterial(keyStore, password.toCharArray()) .build(); // Allow TLSv1 protocol only SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); return getPostText(httpclient, url, param); }catch(Exception e){ e.printStackTrace(); return ""; } } public static String in2Text(InputStream in) throws Exception { ByteArrayOutputStream out=new ByteArrayOutputStream(1024); byte[] buf=new byte[1024]; int len=0; while((len=in.read(buf))>0){ out.write(buf, 0, len); } buf=out.toByteArray(); return new String(buf,"UTF-8"); } }
相关文章推荐
- java继承、接口和抽象类
- java异常概述
- 关于spring 整合 hibernate 事务不回滚
- 项目中使用的spring 注解说明
- 在Eclipse在使用JUnit4单元测试(0基础知识)
- java中静态代码块的用法 static用法详解
- jdk导证书
- Java并发编程笔记 Java线程池的分析和使用
- hadoop2.5.2eclipse插件编译
- java ___ HashCode 和 equals ()
- 【think in java 笔记1】java变量初始化顺序
- Eclipse编译器及一些jdk + notepad
- 观察者模式--Java设计模式
- 项目中使用的spring 注解说明
- 使用Java完成《算法导论》习题2.3-5
- Java中&和&&的区别和联系
- 语料处理中连字符的处理
- Box2d碰撞粘滞的问题(java)
- 使用Java完成《算法导论》习题2.3-4
- how to do graphics in java(quote from big java, 5e)?