您的位置:首页 > 编程语言 > Java开发

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");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: