Java后台对接第三方接口发送数据GET\POST\PUT\SOCKET
工具类
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
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(), StandardCharsets.UTF_8));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
log.error("GET请求异常{}" , e.getMessage());
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.getMessage();
}
}
return result;
}
////////////////////////////////POST/////////////