Android:网络:通过GET和POST两种方式上传参数
2016-01-04 09:36
633 查看
/**
* 通过HttpClient发送数据
* @param title 标题
* @param length 时长
* @return
*/
public static boolean save(String title, String length) {
String path = "http://192.168.0.168:8080/web/ManageServlet";
Map<String, String> params = new HashMap<String, String>();
params.put("title", title);
params.put("timelength", length);
try {
return sendHttpClientPOSTRequest(path, params, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 通过HttpClient发送Post请求
* @param path 请求路径
* @param params 请求参数
* @param encoding 编码
* @return 请求是否成功
*/
private static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
List<NameValuePair> pairs = new ArrayList<NameValuePair>();//存放请求参数
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding);
HttpPost httpPost = new HttpPost(path);
httpPost.setEntity(entity);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200){
return true;
}
return false;
}
/**
* 发送Post请求,参数是请求体
* @param path 请求路径
* @param params 请求参数
* @param encoding 编码
* @return 请求是否成功
*/
private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
// title=liming&timelength=90
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(path).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 true;
}
return false;
}
/**
* 发送GET请求,参数在路径上
* @param path 请求路径
* @param params 请求参数
* @param encoding 编码
* @return 请求是否成功
*/
private static boolean sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{
// http://192.168.1.100:8080/web/ManageServlet?title=xxx&timelength=90
StringBuilder url = new StringBuilder(path);
url.append("?");
for(Map.Entry<String, String> entry : params.entrySet()){
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(), ecoding));
url.append("&");
}
url.deleteCharAt(url.length() - 1);
HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
return true;
}
return false;
}
在Servlet中post和get都使用下面的方式获取数据
String title = request.getParameter("title");
String timelength = request.getParameter("timelength");
System.out.println("视频名称:"+ title);
System.out.println("时长:"+ timelength);
* 通过HttpClient发送数据
* @param title 标题
* @param length 时长
* @return
*/
public static boolean save(String title, String length) {
String path = "http://192.168.0.168:8080/web/ManageServlet";
Map<String, String> params = new HashMap<String, String>();
params.put("title", title);
params.put("timelength", length);
try {
return sendHttpClientPOSTRequest(path, params, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 通过HttpClient发送Post请求
* @param path 请求路径
* @param params 请求参数
* @param encoding 编码
* @return 请求是否成功
*/
private static boolean sendHttpClientPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
List<NameValuePair> pairs = new ArrayList<NameValuePair>();//存放请求参数
if(params!=null && !params.isEmpty()){
for(Map.Entry<String, String> entry : params.entrySet()){
pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encoding);
HttpPost httpPost = new HttpPost(path);
httpPost.setEntity(entity);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(httpPost);
if(response.getStatusLine().getStatusCode() == 200){
return true;
}
return false;
}
/**
* 发送Post请求,参数是请求体
* @param path 请求路径
* @param params 请求参数
* @param encoding 编码
* @return 请求是否成功
*/
private static boolean sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
// title=liming&timelength=90
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(path).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 true;
}
return false;
}
/**
* 发送GET请求,参数在路径上
* @param path 请求路径
* @param params 请求参数
* @param encoding 编码
* @return 请求是否成功
*/
private static boolean sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{
// http://192.168.1.100:8080/web/ManageServlet?title=xxx&timelength=90
StringBuilder url = new StringBuilder(path);
url.append("?");
for(Map.Entry<String, String> entry : params.entrySet()){
url.append(entry.getKey()).append("=");
url.append(URLEncoder.encode(entry.getValue(), ecoding));
url.append("&");
}
url.deleteCharAt(url.length() - 1);
HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
if(conn.getResponseCode() == 200){
return true;
}
return false;
}
在Servlet中post和get都使用下面的方式获取数据
String title = request.getParameter("title");
String timelength = request.getParameter("timelength");
System.out.println("视频名称:"+ title);
System.out.println("时长:"+ timelength);
相关文章推荐
- Android:网络:获取JSON格式数据
- 全站HTTPS底层实现原理
- Android:网络:获取XML格式数据
- Android:网络:网页源码查看器
- Android:网络:图片查看器
- http://snowdream.github.io/awesome-android/
- Apache Karaf开启ssl Https
- TCP协议中的三次握手和四次挥手(图解)
- 五:网络--多线程断点下载
- 四:网络--NSURLConnection基本使用
- 三:网络--数据安全/加密
- 在https的链接中vb使用post提交xml数据
- 二:网络--GET请求和POST请求
- 一:网络--HTTP协议
- 从朴素贝叶斯分类器到贝叶斯网络(下)
- [No000079]罗辑思维2016.1.2日前的所有每日语音,python3做的网络爬虫
- 常看(关于网络配置和添加用户)
- Nagle 与 SO_SNDBUF的问题(转载)
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包
- HttpUtil 参考别人写的