public class HttpUtils {
/**
* Get方式
*/
public static String doGet(String url) {
String result = "";
// try {
// // 当出现乱码时,可能是由于编码错误引起,尝试改变编码格式,使用URLEncoder.encode()方法
// url = url + "?username=" + URLEncoder.encode(name, "UTF-8") + "&pwd=" + URLEncoder.encode(password, "UTF-8");
// } catch (UnsupportedEncodingException e1) {
// e1.printStackTrace();
// }
InputStream is = null;
InputStreamReader isr = null;
BufferedReader bufferedReader = null;
try {
URL httpUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
// 设置读取超时
connection.setReadTimeout(5000);
// 设置链接超时时间
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
// 获得链接请求码
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = connection.getInputStream();
isr = new InputStreamReader(is);
bufferedReader = new BufferedReader(isr);
String str = null;
StringBuffer sb = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
System.out.println("Get方式的result:" + sb.toString());
result = sb.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null)
is.close();
if (isr != null)
isr.close();
if (bufferedReader != null)
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* Post方式
*/
public static String doPost(String url, String content) {
String result = "";
InputStream is = null;
InputStreamReader isr = null;
BufferedReader bufferedReader = null;
try {
// 获得当前系统信息
// Properties properties = System.getProperties();
// properties.list(System.out);
URL httpUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(7000);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
// Content-Length: 请求内容的长度
connection.setRequestProperty("Content-Length", content.getBytes().length + "");
// Log.e("doPost", "content.length(): " + content.getBytes().length );
// Cache-Control: 控制HTTP缓存的方法, 详情可见:
// http://www.cnblogs.com/yuyii/archive/2008/10/16/1312238.html
// connection.setRequestProperty("Cache-Control", "max-age=0");
// Origin: 请求发起者
// connection.setRequestProperty("Origin", "http://192.168.2.124:8080");
OutputStream outputStream = connection.getOutputStream();
// 提交数据
outputStream.write(content.getBytes());
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = connection.getInputStream();
isr = new InputStreamReader(is);
bufferedReader = new BufferedReader(isr);
String str = null;
StringBuffer sb = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
result = sb.toString();
System.out.println("Post方式的result:" + sb.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(is);
close(isr);
close(bufferedReader);
}
return result;
}
public static String doJsonPost(String url, String content) {
String result = "";
InputStream is = null;
InputStreamReader isr = null;
BufferedReader bufferedReader = null;
try {
URL httpUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) httpUrl.openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(7000);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
// Content-Length: 请求内容的长度
connection.setRequestProperty("Content-Length", content.getBytes().length + "");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Token", "Ivj6eZRx40+MTx2ZvnG8nA");
// Log.e("doPost", "content.length(): " + content.getBytes().length );
// Cache-Control: 控制HTTP缓存的方法, 详情可见:
// http://www.cnblogs.com/yuyii/archive/2008/10/16/1312238.html
// connection.setRequestProperty("Cache-Control", "max-age=0");
// Origin: 请求发起者
// connection.setRequestProperty("Origin", "http://192.168.2.124:8080");
OutputStream outputStream = connection.getOutputStream();
// 提交数据
outputStream.write(content.getBytes());
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = connection.getInputStream();
isr = new InputStreamReader(is);
bufferedReader = new BufferedReader(isr);
String str = null;
StringBuffer sb = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
sb.append(str);
}
result = sb.toString();
System.out.println("Post方式的result:" + sb.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
close(is);
close(isr);
close(bufferedReader);
}
return result;
}
/**
* 关闭流
*/
public static boolean close(Closeable io) {
if (io != null) {
try {
io.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}
Http请求工具类
最新推荐文章于 2024-08-02 03:52:20 发布