package com.itemutils.http;
import com.alibaba.fastjson.JSON;
import com.itemutils.http.apache.HttpConstant;
import com.itemutils.http.apache.HttpType;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
/**
-
@Author zbf
-
@DATA 2019/9/1 18:21
-
@ClassIntroduction
*/
public class UseHttpUtil {private static final String post=“POST”;
private static final String get=“GET”;/**
- get 请求
- @param url 请求url
- @param mapHeader 请求头信息
- @return
*/
public static String get(String url,Map<String,String> mapHeader){
HttpURLConnection connection=null;
InputStream inputStream=null;
BufferedReader reader=null;
String result=null;
try {
URL url1 = new URL(url);
//打开连接
connection = (HttpURLConnection) url1.openConnection();
//设置请求方式
connection.setRequestMethod(get);
//设置超时时间
connection.setConnectTimeout(15000);
//设置读取数据的时间
connection.setReadTimeout(90000);
//设置请求头参数
if (mapHeadernull){
//默认的请求 JSON(“Content-Type”,“application/json”),
connection.setRequestProperty(HttpType.JSON.getName(),HttpType.JSON.getType());
}else {
HttpURLConnection finalConnection = connection;
mapHeader.entrySet().forEach(e-> finalConnection.setRequestProperty(e.getKey(),e.getValue()));
}
connection.connect();
if (200connection.getResponseCode()){
inputStream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, HttpConstant.UTF_8));
StringBuilder sbd = new StringBuilder();
String temp=null;
while (null!=(temp=reader.readLine())){
sbd.append(temp);
sbd.append("\r\n");
}
result=sbd.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
inputStream.close();
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
- post请求
- @param url 请求地址
- @param mapHeader 请求头信息
- @param mapBody 请求参数
- @return
*/
public static String post(String url, Map<String,String> mapHeader,Map<String,String> mapBody){
HttpURLConnection connection=null;
OutputStream outputStream=null;
InputStream inputStream=null;
BufferedReader reader=null;
String result=null;
try {
URL url1 = new URL(url);
//打开远程连接
connection = (HttpURLConnection) url1.openConnection();
//设置请求方式
connection.setRequestMethod(post);
//设置请求连接的超时的时间15s
connection.setConnectTimeout(15000);
//设置远程数据返回的时间90s
connection.setReadTimeout(90000);
//默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
connection.setDoOutput(true);
//设置请求头参数
if (mapHeadernull){
//默认的请求 JSON(“Content-Type”,“application/json”),
connection.setRequestProperty(HttpType.JSON.getName(),HttpType.JSON.getType());
}else {
HttpURLConnection finalConnection = connection;
mapHeader.entrySet().forEach(e-> finalConnection.setRequestProperty(e.getKey(),e.getValue()));
}
//通过连接对象获取输出流
outputStream= connection.getOutputStream();
//转换成字符数据传送出去
outputStream.write(JSON.toJSONBytes(mapBody));
if (200connection.getResponseCode()){
inputStream=connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, HttpConstant.UTF_8));
StringBuilder sbd = new StringBuilder();
String temp=null;
while (null!=(temp=reader.readLine())){
sbd.append(temp);
sbd.append("\r\n");
}
result=sbd.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
inputStream.close();
outputStream.close();
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}