java后端原生发送http请求方法
public static String sendPost(String httpUrl, Map<String, String> map) {
HttpURLConnection connection = null;
InputStream is = null;
OutputStream os = null;
BufferedReader br = null;
String result = null;
try {
StringBuffer param = new StringBuffer();
boolean isFristPar = true;
for(String key : map.keySet()){
if(isFristPar == false){
param.append("&");
}
isFristPar = false;
String val = URLEncoder.encode(map.get(key),"UTF-8");
param.append(String.format("%1$s=%2$s",key,val));
}
URL url = new URL(httpUrl);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
os = connection.getOutputStream();
os.write(param.toString().getBytes());
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
log.error("{}",e);
} catch (IOException e) {
log.error("{}",e);
} finally {
if (null != br) {
try {
br.close();
} catch (IOException e) {
log.error("{}",e);
}
}
if (null != os) {
try {
os.close();
} catch (IOException e) {
log.error("{}",e);
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
log.error("{}",e);
}
}
connection.disconnect();
}
return result;
}
public static String sendPostBody(String url, Map<String, String> map){
OutputStreamWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
if (map.equals("") == false) {
JSONObject jsonObject = new JSONObject();
for (Map.Entry<String, String> mt : map.entrySet()){
jsonObject.put(mt.getKey(),mt.getValue());
}
out.write(jsonObject.toJSONString());
}
out.flush();
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "utf-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
log.error("============>发送请求异常!");
log.error("",e);
return null;
}
finally{
try{
if(in!=null){
in.close();
}
if(out!=null){
out.close();
}
}
catch(IOException e){
log.error("{}",e);
}
}
return result;
}
public static String httpGet(String getUrl) throws Exception {
StringBuffer buffer = new StringBuffer();
HttpURLConnection httpUrlConn = null;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
URL url = new URL(getUrl);
httpUrlConn = (HttpURLConnection) url.openConnection();
httpUrlConn.setDoOutput(false);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
httpUrlConn.setRequestMethod("GET");
httpUrlConn.connect();
inputStream = httpUrlConn.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, "utf-8");
bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
} catch (Exception e) {
throw new Exception("HttpGet异常",e);
}finally {
try{
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
httpUrlConn.disconnect();
}catch (Exception ex){}
}
return buffer.toString();
}