* @param request
* 请求中需要的参数对象
* @return 返回请求相应的字符串
*/
public static String doRequest(Request request) {
try {
URL url = new URL(request.getUrl());
URLConnection conn = url.openConnection();
HttpURLConnection connection = (HttpURLConnection) conn;
connection.setDoOutput(true);
connection.setConnectTimeout(100000);
connection.setRequestMethod(request.getMethod());
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true); // 设置头信息
Map requestHeadMap = request.getRequestHeadMap();
Set mapSet = requestHeadMap.keySet();
for (String set : mapSet) {
connection.setRequestProperty(set, requestHeadMap.get(set));
}
if (request.getMethod().equals(Request.METHOD_POST)) {
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), request.getEncode());
out.write(request.getContent());
out.flush();
out.close();
}
// 获取相应
String lineTemp = "";
String responseStr = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((lineTemp = reader.readLine()) != null) {
if (!"".equals(lineTemp)) {
responseStr += lineTemp + "\r\n";
}
}
return responseStr;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}}/**
* @author Kevin
*
*/
class Request {
/**
* @param url
* 请求的url
* @param method
* 请求的方法
*/
public Request(String url, String method) {
this(url, method, "", "UTF-8");
} /**
* @param url
* 请求的url
* @param method
* 请求的方法
* @param content
* 请求体
*/
public Request(String url, String method, String content) {
this(url, method, content, "UTF-8");
} /**
* 构造方法
*
* @param url
* 请求的url
* @param method
* 请求的方法
* @param content
* 请求体
* @param encode
* 请求使用的编码
*/
public Request(String url, String method, String content, String encode) {
this.url = this.baseUrl + url;
this.method = method;
this.content = content;
this.encode = encode; requestHeadMap = new HashMap();
requestHeadMap.put("version", "international_eub_us_1.1");// 版本信息
requestHeadMap.put("authenticate", "用户授权码");// 用户授权
} /**
*
*/
public static String METHOD_POST = "POST"; /**
* get方法
*/
public static String METHOD_GET = "GET"; /**
* delete方法
*/
public static String METHOD_DELETE = "DELETE"; /**
* 服务地址
*/
private String baseUrl = "http://www.ems.com.cn/partner/"; /**
* 请求的各个服务地址
*/
private String url = ""; /**
* 请求的方法
*/
private String method = ""; /**
* POST请求的请求体
*/
private String content = ""; /**
* 编码
*/
private String encode = ""; /**
* 保存请求的头信息
*/
private Map requestHeadMap = null; /**
* @return the baseUrl
*/
public String getBaseUrl() {
return baseUrl;
} /**
* @param baseUrl
* the baseUrl to set
*/
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
} /**
* @return the url
*/
public String getUrl() {
return url;
} /**
* @param url
* the url to set
*/
public void setUrl(String url) {
this.url = url;
} /**
* @return the method
*/
public String getMethod() {
return method;
} /**
* @param method
* the method to set
*/
public void setMethod(String method) {
this.method = method;
} /**
* @return the content
*/
public String getContent() {
return content;
} /**
* @param content
* the content to set
*/
public void setContent(String content) {
this.content = content;
} /**
* @return the encode
*/
public String getEncode() {
return encode;
} /**
* @param encode
* the encode to set
*/
public void setEncode(String encode) {
this.encode = encode;
} /**
* @return the requestHeadMap
*/
public Map getRequestHeadMap() {
return requestHeadMap;
} /**
* @param requestHeadMap
* the requestHeadMap to set
*/
public void setRequestHeadMap(Map requestHeadMap) {
this.requestHeadMap = requestHeadMap;
}
}接上贴代码
* 请求中需要的参数对象
* @return 返回请求相应的字符串
*/
public static String doRequest(Request request) {
try {
URL url = new URL(request.getUrl());
URLConnection conn = url.openConnection();
HttpURLConnection connection = (HttpURLConnection) conn;
connection.setDoOutput(true);
connection.setConnectTimeout(100000);
connection.setRequestMethod(request.getMethod());
// 发送POST请求必须设置如下两行
connection.setDoOutput(true);
connection.setDoInput(true); // 设置头信息
Map requestHeadMap = request.getRequestHeadMap();
Set mapSet = requestHeadMap.keySet();
for (String set : mapSet) {
connection.setRequestProperty(set, requestHeadMap.get(set));
}
if (request.getMethod().equals(Request.METHOD_POST)) {
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), request.getEncode());
out.write(request.getContent());
out.flush();
out.close();
}
// 获取相应
String lineTemp = "";
String responseStr = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((lineTemp = reader.readLine()) != null) {
if (!"".equals(lineTemp)) {
responseStr += lineTemp + "\r\n";
}
}
return responseStr;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}}/**
* @author Kevin
*
*/
class Request {
/**
* @param url
* 请求的url
* @param method
* 请求的方法
*/
public Request(String url, String method) {
this(url, method, "", "UTF-8");
} /**
* @param url
* 请求的url
* @param method
* 请求的方法
* @param content
* 请求体
*/
public Request(String url, String method, String content) {
this(url, method, content, "UTF-8");
} /**
* 构造方法
*
* @param url
* 请求的url
* @param method
* 请求的方法
* @param content
* 请求体
* @param encode
* 请求使用的编码
*/
public Request(String url, String method, String content, String encode) {
this.url = this.baseUrl + url;
this.method = method;
this.content = content;
this.encode = encode; requestHeadMap = new HashMap();
requestHeadMap.put("version", "international_eub_us_1.1");// 版本信息
requestHeadMap.put("authenticate", "用户授权码");// 用户授权
} /**
*
*/
public static String METHOD_POST = "POST"; /**
* get方法
*/
public static String METHOD_GET = "GET"; /**
* delete方法
*/
public static String METHOD_DELETE = "DELETE"; /**
* 服务地址
*/
private String baseUrl = "http://www.ems.com.cn/partner/"; /**
* 请求的各个服务地址
*/
private String url = ""; /**
* 请求的方法
*/
private String method = ""; /**
* POST请求的请求体
*/
private String content = ""; /**
* 编码
*/
private String encode = ""; /**
* 保存请求的头信息
*/
private Map requestHeadMap = null; /**
* @return the baseUrl
*/
public String getBaseUrl() {
return baseUrl;
} /**
* @param baseUrl
* the baseUrl to set
*/
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
} /**
* @return the url
*/
public String getUrl() {
return url;
} /**
* @param url
* the url to set
*/
public void setUrl(String url) {
this.url = url;
} /**
* @return the method
*/
public String getMethod() {
return method;
} /**
* @param method
* the method to set
*/
public void setMethod(String method) {
this.method = method;
} /**
* @return the content
*/
public String getContent() {
return content;
} /**
* @param content
* the content to set
*/
public void setContent(String content) {
this.content = content;
} /**
* @return the encode
*/
public String getEncode() {
return encode;
} /**
* @param encode
* the encode to set
*/
public void setEncode(String encode) {
this.encode = encode;
} /**
* @return the requestHeadMap
*/
public Map getRequestHeadMap() {
return requestHeadMap;
} /**
* @param requestHeadMap
* the requestHeadMap to set
*/
public void setRequestHeadMap(Map requestHeadMap) {
this.requestHeadMap = requestHeadMap;
}
}接上贴代码
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货