第一个工具类
package com.ty.mini.config;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
public class PostAndGet {
public static String SendPost(String param,String url){
PrintStream out=null;
BufferedReader in=null;
String result="";
try {
URL realUrl=new URL(url);
URLConnection connection=realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setDoOutput(true);
connection.setDoInput(true);
// 获取URLConnection对象对应的输出流
out=new PrintStream(connection.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in=new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line=in.readLine())!=null){
result+=line;
}
} catch (Exception e) {
System.out.println("[POST请求]向地址:" + url + " 发送数据:" + param + " 发生错误!");
}
finally {// 使用finally块来关闭输出流、输入流
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
System.out.println("关闭流异常");
}
}
return result;
}
public static Object sendGet(String url){
String result="";
BufferedReader in=null;
try {
URL realUrl=new URL(url);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
connection.connect();
// 定义BufferedReader输入流来读取URL的响应
in=new BufferedReader(new InputStreamReader(connection.getInputStream(),"utf-8"));
String line;
while ((line=in.readLine())!=null){
result+=line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
}finally {
try {
if(in==null){
in.close();
}
} catch (IOException e) {
System.out.println("关闭流异常");
}
}
return result;
}
}
第二个工具类(比较全面)
package com.business.common.util;
import org.apache.http.Consts;
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.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpClientUtils {
private static final Logger log = LoggerFactory.getLogger(HttpClientUtils.class);
private static final CloseableHttpClient httpclient = HttpClients.createDefault();
private static final String userAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.87 Safari/537.36";
public static String sendGet(String url, String token) {
String result = null;
CloseableHttpResponse response = null;
try {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("User-Agent", userAgent);
httpGet.setHeader("Authorization", "token " + token);
response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
} catch (Exception e) {
log.error("处理失败 {}" + e);
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return result;
}
public static String sendPost(String url, Map<String, String> map) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
for (Map.Entry<String, String> entry : map.entrySet()) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", userAgent);
httpPost.setEntity(formEntity);
CloseableHttpResponse response = null;
String result = null;
try {
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return result;
}
public static String sendPost(String url, String jsonStr) {
String result = null;
StringEntity entity = new StringEntity(jsonStr, Consts.UTF_8);
entity.setContentType("application/json");
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", userAgent);
httpPost.setHeader("Accept", "application/json");
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
result = EntityUtils.toString(httpEntity);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return result;
}
public static String sendPost(String url) {
String result = null;
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("User-Agent", userAgent);
CloseableHttpResponse response = null;
try {
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
return result;
}
}