android端向服务器提交请求的几种方式
1、GET方式
其实GET方式说白了,就是拼接字符串。。最后拼成的字符串的格式是: path ? username= ....& password= ......
public boolean loginByGet(String path, String username , String password) throws Exception{ String url_path = path +"?username=" + URLEncoder.encode(username, "utf-8") + "&password="+password; URL url = new URL(url_path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
if(conn.getResponseCode() == 200){
return true;
} return false;
}
2、POST方式
post方式中,一定要设置以下两个请求参数
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", entity.length + "");
完整代码如下:
public boolean loginByPost(String path,String username , String password) throws Exception{ System.out.println("LoginService的loginByPost()");
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST");
conn.setConnectTimeout(5000); String value = "username=" + username +"&" + "password=" +password;
byte[] entity = value.getBytes(); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", entity.length + ""); conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
os.write(entity); if(conn.getResponseCode() == 200){
return true;
} return false;
}
3、通过HttpClient以GET方式提交请求
使用HttpClient这个第三方框架以后,我们在编写代码的时候就看不到URL、conn之类的。他其实就是对Http协议进行了封装
public boolean loginByHttpClientGet(String path,String username , String password) throws Exception{ String value = path + "?username=" + username +"&password=" + password;
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(value); HttpResponse httpResponse = httpClient.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode() == 200){
return true;
} return false;
}
4、通过HttpClient以POST方式提交请求
public boolean loginByHttpClientPost(String path,String username , String password)throws Exception{ HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(path); List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("username", username));
parameters.add(new BasicNameValuePair("password", password)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");
httpPost.setEntity(entity);
HttpResponse httpResponse = httpClient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200){
return true;
} return false;
}
android端向服务器提交请求的几种方式的更多相关文章
- android端从服务器抓取的几种常见的数据的处理方式
1.图片 public void look(View v) { String path = et_path.getText().toString(); try { URL url = new URL( ...
- android 向serverGet和Post请求的两种方式,android向server发送文件,自己组装协议和借助第三方开源
一个适用于Android平台的第三方和apache的非常多东西类似,仅仅是用于Android上 我在项目里用的是这个 https://github.com/loopj/android-async-ht ...
- android中用get和post方式向服务器提交请求
通过get和post方式向服务器发送请求首先说一下get和post的区别get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23 ...
- android客户端向服务器发送请求中文乱码的问
android客户端向服务器发送请求的时候,并将参数保存到数据库时遇到了中文乱码的问题: 解决方法: url = "http://xxxx.com/Orders/saveorder.html ...
- 讨论HTTP POST 提交数据的几种方式
转自:http://www.cnblogs.com/softidea/p/5745369.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PU ...
- C#中Post请求的两种方式发送参数链和Body的
POST请求 有两种方式 一种是组装key=value这种参数对的方式 一种是直接把一个字符串发送过去 作为body的方式 我们在postman中可以看到 sfdsafd sdfsdfds publi ...
- 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式
一. 相关概念介绍 1. SSL证书服务 SSL证书服务由"服务商"联合多家国内外数字证书管理和颁发的权威机构.在xx云平台上直接提供的服务器数字证书.您可以在阿里云.腾讯云等平台 ...
- 怎样在Android开发中FPS游戏实现的两种方式比较
怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
随机推荐
- Python常用模块中常用内置函数的具体介绍
Python作为计算机语言中常用的语言,它具有十分强大的功能,但是你知道Python常用模块I的内置模块中常用内置函数都包括哪些具体的函数吗?以下的文章就是对Python常用模块I的内置模块的常用内置 ...
- 动态网页爬取例子(WebCollector+selenium+phantomjs)
目标:动态网页爬取 说明:这里的动态网页指几种可能:1)需要用户交互,如常见的登录操作:2)网页通过JS / AJAX动态生成,如一个html里有<div id="test" ...
- itextSharp 使用模板(PdfTemplate)不规则分栏(ColumnText)
public static void Main() { Document document = new Document(); BaseFont bf = BaseFont.createFont(Ba ...
- Linux的五个查找命令 [转]
最近,我在学习Linux,下面是一些笔记. 使用电脑的时候,经常需要查找文件. 在Linux中,有很多方法可以做到这一点.国外网站LinuxHaxor总结了五条命令,你可以看看自己知道几条.大多数程序 ...
- Cookie 路径在本机测试及服务器部署,在浏览器处理方式上的不同
Table of Contents 1 问题场景 2 解决过程 2.1 cookie是否设置成功 2.2 cookie是否上传到服务器 3 总结 1 问题场景 最近在学用Python进行web开发,写 ...
- 在Ubuntu 12.04下编译qtiplot
不在windows下,再加上不想用盗版,所以需要一个origin的替代品——qtiplot.虽然我非常抵抗用这种不停点来点去的软件,用R的ggplot2画图多好啊,高效.优雅.漂亮,但是终抵不过老板一 ...
- Writing a Windows Shell Extension(marco cantu的博客)
Writing a Windows Shell Extension This is a technical article covering the content of my last week s ...
- 1.0.1-学习Opencv与MFC混合编程之---播放AVI视频
资源源代码:http://download.csdn.net/detail/nuptboyzhb/3961639 版本1.0.1新增内容 Ø 新建菜单项,Learning OpenCV——> ...
- 【免费讲座IX算法第一阶段】转专业找CS工作“打狗棒法”
个人经验CS不相干,如何收拾简历?如何获取知识,在最短的时间内找到一份工作需要?如何避免盲目刷称号,迅速制定学习计划?如何准备面试? 星期五.九算法黄蓉老师受邀嘉宾 [在线共享] 她成功转专业的六个月 ...
- Swift - 给游戏添加背景音乐和音效(SpriteKit游戏开发)
游戏少不了背景音乐和音效.下面我们通过创建一个管理音效的类,来实现背景音乐的播放,同时点击屏幕可以播放相应的音效. 声音管理类 SoundManager.swift 1 2 3 4 5 6 7 8 9 ...