Get方法
public String doGet(String url, String... param) {
String result = "";
BufferedReader in = null;
System.setProperty("jsse.enableSNIExtension", "false");
try {
String urlstring = url;
for (String s : param) {
urlstring += s;
}
URL realUrl = new URL(urlstring);
URLConnection connection = realUrl.openConnection();
connection.setRequestProperty("Charset", "UTF-8");
connection.connect();
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送get请求失败" + e);
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
post方法
public String doPost(String testUrl, String param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
System.setProperty("jsse.enableSNIExtension", "false");
try {
URL url = new URL(testUrl);
URLConnection urlConnection = url.openConnection();
urlConnection.setRequestProperty("Charset", "UTF-8");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
out = new PrintWriter(urlConnection.getOutputStream());
out.print(param);
out.flush();
in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (IOException e) {
System.out.println("发送POST请求失败");
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}