URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接。程序可以通过URLConnection实例向该URL发送请求、读取URL引用的资源。
通常创建一个和 URL 的连接,并发送请求、读取此 URL 引用的资源需要如下几个步骤:
(1)通过调用URL对象openConnection()方法来创建URLConnection对象。
(2)设置URLConnection的参数和普通请求属性。
(3)如果只是发送GET方式请求,使用connect方法建立和远程资源之间的实际连接即可;如果需要发送POST方式的请求,需要获取URLConnection实例对应的输出流来发送请求参数。
(4)远程资源变为可用,程序可以访问远程资源的头字段、或通过输入流读取远程资源的数据。
-
package cn.edu.zzu.wemall.net;
-
-
import java.io.BufferedReader;
-
import java.io.InputStreamReader;
-
import java.io.PrintWriter;
-
import java.net.URL;
-
import java.net.URLConnection;
-
import java.util.List;
-
import java.util.Map;
-
-
import cn.edu.zzu.wemall.config.MyConfig;
-
-
public class HttpRequest {
-
/**
-
* 向指定URL发送GET方法的请求
-
*
-
* @param url
-
* 发送请求的URL
-
* @param param
-
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
-
* @return URL 所代表远程资源的响应结果
-
* @throws Exception
-
*
-
* modify by liudewei
-
*/
-
@SuppressWarnings("unused")
-
public static String sendGetWithParameter(String url, String param) throws Exception {
-
String result = "";
-
BufferedReader in = null;
-
try {
-
String urlNameString = url + "?" + param;
-
URL realUrl = new URL(urlNameString);
-
// 打开和URL之间的连接
-
URLConnection connection = realUrl.openConnection();
-
// 设置通用的请求属性
-
-
connection.setRequestProperty("user-agent",
-
MyConfig.ClientUserAgent);
-
connection.setConnectTimeout(5000);
-
// 建立实际的连接
-
connection.connect();
-
// 获取所有响应头字段
-
Map<String, List<String>> map = connection.getHeaderFields();
-
// 遍历所有的响应头字段
-
for (String key : map.keySet()) {
-
// System.out.println(key + "--->" + map.get(key)); //关闭响应头的输出
-
}
-
// 定义 BufferedReader输入流来读取URL的响应
-
in = new BufferedReader(new InputStreamReader(
-
connection.getInputStream()));
-
String line;
-
while ((line = in.readLine()) != null) {
-
result += line;
-
}
-
} catch (Exception e) {
-
throw e;
-
}
-
// 使用finally块来关闭输入流
-
finally {
-
try {
-
if (in != null) {
-
in.close();
-
}
-
} catch (Exception e2) {
-
throw e2;
-
}
-
}
-
return result;
-
}
-
-
-
-
@SuppressWarnings("unused")
-
public static String sendGet(String url) throws Exception {
-
String result = "";
-
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",
-
MyConfig.ClientUserAgent);
-
connection.setConnectTimeout(5000);
-
// 建立实际的连接
-
connection.connect();
-
// 获取所有响应头字段
-
Map<String, List<String>> map = connection.getHeaderFields();
-
// 遍历所有的响应头字段
-
for (String key : map.keySet()) {
-
// System.out.println(key + "--->" + map.get(key));
-
}
-
// 定义 BufferedReader输入流来读取URL的响应
-
in = new BufferedReader(new InputStreamReader(
-
connection.getInputStream()));
-
String line;
-
while ((line = in.readLine()) != null) {
-
result += line;
-
}
-
} catch (Exception e) {
-
throw e;
-
}
-
// 使用finally块来关闭输入流
-
finally {
-
try {
-
if (in != null) {
-
in.close();
-
}
-
} catch (Exception e2) {
-
throw e2;
-
}
-
}
-
return result;
-
}
-
-
/**
-
* 向指定 URL 发送POST方法的请求
-
*
-
* @param url
-
* 发送请求的 URL
-
* @param param
-
* 请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
-
* @return 所代表远程资源的响应结果
-
* @throws Exception
-
*/
-
public static String sendPost(String url, String param) throws Exception {
-
PrintWriter out = null;
-
BufferedReader in = null;
-
String result = "";
-
try {
-
URL realUrl = new URL(url);
-
// 打开和URL之间的连接
-
URLConnection conn = realUrl.openConnection();
-
// 设置通用的请求属性
-
conn.setRequestProperty("accept", "*/*");
-
conn.setRequestProperty("connection", "Keep-Alive");
-
conn.setRequestProperty("user-agent",
-
MyConfig.ClientUserAgent);
-
conn.setConnectTimeout(5000);
-
// 发送POST请求必须设置如下两行
-
conn.setDoOutput(true);
-
conn.setDoInput(true);
-
// 获取URLConnection对象对应的输出流
-
out = new PrintWriter(conn.getOutputStream());
-
// 发送请求参数
-
out.print(param);
-
// flush输出流的缓冲
-
out.flush();
-
// 定义BufferedReader输入流来读取URL的响应
-
in = new BufferedReader(
-
new InputStreamReader(conn.getInputStream()));
-
String line;
-
while ((line = in.readLine()) != null) {
-
result += line;
-
}
-
} catch (Exception e) {
-
throw e;
-
}
-
//使用finally块来关闭输出流、输入流
-
finally{
-
try{
-
if(out!=null){
-
out.close();
-
}
-
if(in!=null){
-
in.close();
-
}
-
}
-
catch(Exception ex){
-
throw ex;
-
}
-
}
-
return result;
-
}
-
}
原文详情地址:
wemall doraemonAndroid app商城详情地址:
wemall官网地址:
wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统
阅读(600) | 评论(0) | 转发(0) |