android端向服务器提交请求的几种方式
2024-10-14 06:13:41
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;
}
最新文章
- table居中
- 高仿精仿手机版QQ空间应用源码
- 洛谷P1901 发射站
- IM即时通讯
- iOS开发多线程篇—NSOperation基本操作
- PHP接口使用
- C# 中使用win32函数 GetScrollInfo返回false 返回引用全是零的问题
- JavaScript之共享onload
- 团队项目汇总beta
- 2.python的文件类型、变量数值和字符串练习
- 第25章 退出外部身份提供商 - Identity Server 4 中文文档(v1.0.0)
- Jmeter笔记(Ⅱ)使用Jmeter实现轻量级的接口自动化测试
- appium+python3+pycharm踩得坑2
- Linux 驱动——Button驱动1
- Ubuntu16.04 下 hadoop的安装与配置(伪分布式环境)
- Js封装的动画函数实现轮播图
- 学以致用二十八-----win10安装mysql5.7.24及卸载
- shell基础:环境变量
- codeforces水题100道 第二十一题 Codeforces Beta Round #65 (Div. 2) A. Way Too Long Words (strings)
- 05_Flume_timestamp interceptor实践
热门文章
- Creating Spatial Indexes(mysql 创建空间索引 The used table type doesn&#39;t support SPATIAL indexes)
- J2EE基础篇——十三个规范
- 使用ExifInterface获取图片信息
- Java Socket实现HTTP客户端来理解Session和Cookie的区别和联系
- The parent project must have a packaging type of POM
- cocos2d-x游戏开发系列教程-坦克大战游戏之坦克的显示
- IntelliJ Idea 经常使用快捷键列表
- hdu1503
- Android Sqlite数据库执行插入查询更新删除的操作对比
- ActivityManager