Android HTTPpost和get请求
来源:互联网 发布:淘宝客服模板在线制作 编辑:程序博客网 时间:2024/04/26 02:09
/**
* 发送Post请求
*
* @param path
* 请求路径
* @param params
* 请求参数
* @param encoding
* 编码
* @return 服务器返回流
*/
public static InputStream sendPOSTRequest(String url,
Map<String, String> params, String encoding) throws Exception {
StringBuilder data = new StringBuilder();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
data.append(entry.getKey()).append("=");
data.append(URLEncoder.encode(entry.getValue(), encoding));
data.append("&");
}
data.deleteCharAt(data.length() - 1);
}
byte[] entity = data.toString().getBytes();// 生成实体数据
HttpURLConnection conn = (HttpURLConnection) new URL(url)
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);// 允许对外输出数据
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entity);
if (conn.getResponseCode() == 200) {
return conn.getInputStream();
}
return null;
}
/**
* 发送get请求的方法
* @param url
* @param params
* @param encoding
* @return
* @throws Exception
*/
public static InputStream sendGETRuqest(String url,
Map<String, String> params, String encoding) throws Exception {
// StringBuilder是用来组拼请求地址和参数
StringBuilder sb = new StringBuilder();
sb.append(url).append("?");
if (params != null && params.size() != 0) {
for (Map.Entry<String, String> entry : params.entrySet()) {
// 如果请求参数中有中文,需要进行URLEncoder编码
sb.append(entry.getKey()).append("=")
.append(URLEncoder.encode(entry.getValue(), encoding));
sb.append("&");
}
sb.deleteCharAt(sb.length() - 1);
}
HttpURLConnection conn = (HttpURLConnection) new URL(sb.toString())
.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
return conn.getInputStream();
}
return null;
}
- Android HTTPpost和get请求
- android 提交httpGet和httpPost请求
- android httpPost 的请求
- Android中两种请求方式HttpGet和HttpPost验证用户登录
- Android HTTPPOST方法请求数据
- Android HTTPPOST方法请求数据
- get和post请求,Android
- android get和post请求
- android通过httpPost向服务器发送请求
- android通过httpPost向服务器发送请求 .
- android通过httpPost向服务器发送请求
- android平台HttpGet、HttpPost请求实例
- Android平台HttpGet、HttpPost请求实例
- Android平台HttpGet、HttpPost请求实例
- Java的HttpClient、HttpGet和HttpPost请求
- android的HttpGet和HttpPost
- Android关于GET和POST发送请求
- Android发送GET和POST请求DEMO
- ios学习分享的网站
- 网页JAVASCRIPT常用命令
- swf属性
- Skype不适合eBay业务面临出售
- 工信部:WiMAX在中国不可使用
- Android HTTPpost和get请求
- 联想控股挂牌转让石药集团
- 手机泡文,经典评论
- ORACLE ORA-01034: ORACLE not available解决
- 武直11直升机高调亮相(图)
- 全球前十上市公司中石油失首
- 正定县召开京石铁路客运专线征地拆迁工作会议
- SQL SERVER DBA 看性能 语句
- 2008年正定县重点项目