Springboot http请求
2020-10-24 本文已影响0人
DragonRat
@Override
@Async()
public String httpFormData(String path, HashMap<String, String> formMap) {
String result = "";
HttpURLConnection connection = null;
String boundary = "--------------------------132183525382215881770481";
try {
URL url = new URL(cvbsHost+path);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
// 不使用缓存
connection.setUseCaches(false);
StringBuffer formSB = new StringBuffer();
if (formMap != null) {
if (formMap.size() > 0) {
for (Map.Entry<String, String> entry : formMap.entrySet()) {
String inputName = entry.getKey();
String inputValue = entry.getValue();
formSB.append("\r\n").append("--").append(boundary).append("\r\n");
formSB.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");
formSB.append(inputValue);
}
formSB.append("\r\n").append("--").append(boundary).append("--");
}
}
connection.connect();
PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), "UTF-8"));
out.print(formSB.toString());
out.flush();
//获得响应状态
int resultCode = connection.getResponseCode();
if (HttpURLConnection.HTTP_OK == resultCode) {
formSB = new StringBuffer();
String readLine;
BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
while ((readLine = responseReader.readLine()) != null) {
formSB.append(readLine).append("\n");
}
responseReader.close();
result = formSB.toString();
} else {
result = "{\"code\":\"" + resultCode + "\"}";
}
out.close();
} catch (Exception e) {
logger.info("[ ~~serviceMpt4uAndmccmForm~~ fail~~ ]{}", e.toString());
return "{\"code\":500,\"result\":\"" + "POST表单请求 " + path + " 时出现异常\"}";
} finally {
connection.disconnect();
}
logger.info("[ ~~serviceMpt4uAndmccmForm~~ fail~~ ]{}", result);
return result;
}
@Override
public String httpFormUrlencoded(String path,String objectName) {
String result;
HttpURLConnection connection = null;
try {
URL url = new URL(cvbsHost+path);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Connection", "Keep-Alive");
// 不使用缓存
connection.setUseCaches(false);
connection.connect();
PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8));
out.print(objectName);
out.flush();
int resultCode = connection.getResponseCode();
if (HttpURLConnection.HTTP_OK == resultCode) {
StringBuffer stringBuffer = new StringBuffer();
String readLine;
BufferedReader responseReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8));
while ((readLine = responseReader.readLine()) != null) {
stringBuffer.append(readLine).append("\n");
}
responseReader.close();
result = stringBuffer.toString();
} else {
result = "{\"code\":\"" + resultCode + "\"}";
}
out.close();
} catch (Exception e) {
logger.error(e.toString());
return "{\"code\":500,\"result\":\"x-www-form-urlencoded请求 " + cvbsHost+"/QuerySubsInfoReq" + " 时出现异常\"}";
} finally {
if (connection != null) {
connection.disconnect();
}
}
return result;
}