package cn.exrick.xboot.your.util;
|
import java.io.*;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.net.URLConnection;
|
|
public class HttpUtil {
|
|
public static String get(String url) {
|
BufferedReader in = null;
|
try {
|
URL realUrl = new URL(url);
|
// 打开和URL之间的连接
|
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.setConnectTimeout(5000);
|
connection.setReadTimeout(5000);
|
// 建立实际的连接
|
connection.connect();
|
// 定义 BufferedReader输入流来读取URL的响应
|
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
StringBuffer sb = new StringBuffer();
|
String line;
|
while ((line = in.readLine()) != null) {
|
sb.append(line);
|
}
|
return sb.toString();
|
} catch (Exception e) {
|
}
|
// 使用finally块来关闭输入流
|
finally {
|
try {
|
if (in != null) {
|
in.close();
|
}
|
} catch (Exception e2) {
|
e2.printStackTrace();
|
}
|
}
|
return null;
|
}
|
|
public static String jsonPost2(String params,String strURL) {
|
try {
|
URL url = new URL(strURL);// 创建连接
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
connection.setUseCaches(false);
|
connection.setInstanceFollowRedirects(true);
|
connection.setRequestMethod("POST"); // 设置请求方式
|
connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
|
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
|
connection.connect();
|
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
|
out.append(params);
|
out.flush();
|
out.close();
|
|
int code = connection.getResponseCode();
|
InputStream is = null;
|
if (code == 200) {
|
is = connection.getInputStream();
|
} else {
|
is = connection.getErrorStream();
|
}
|
BufferedReader bReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
String line, resultStr = "";
|
while (null != (line = bReader.readLine())) {
|
resultStr += line;
|
}
|
bReader.close();
|
return resultStr;
|
} catch (IOException e) {
|
return "error";
|
}
|
}
|
|
public static String jsonPost3(StringBuffer params,String strURL) {
|
try {
|
URL url = new URL(strURL);// 创建连接
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
connection.setDoOutput(true);
|
connection.setDoInput(true);
|
connection.setUseCaches(false);
|
connection.setInstanceFollowRedirects(true);
|
connection.setRequestMethod("POST"); // 设置请求方式
|
connection.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
|
connection.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
|
connection.connect();
|
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); // utf-8编码
|
out.append(params);
|
out.flush();
|
out.close();
|
|
int code = connection.getResponseCode();
|
InputStream is = null;
|
if (code == 200) {
|
is = connection.getInputStream();
|
} else {
|
is = connection.getErrorStream();
|
}
|
|
BufferedReader bReader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
|
String line, resultStr = "";
|
while (null != (line = bReader.readLine())) {
|
resultStr += line;
|
}
|
//System.out.println(resultStr);
|
bReader.close();
|
return resultStr;
|
} catch (IOException e) {
|
return "error";
|
}
|
}
|
|
}
|