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端向服务器提交请求的几种方式的更多相关文章

  1. android端从服务器抓取的几种常见的数据的处理方式

    1.图片 public void look(View v) { String path = et_path.getText().toString(); try { URL url = new URL( ...

  2. android 向serverGet和Post请求的两种方式,android向server发送文件,自己组装协议和借助第三方开源

    一个适用于Android平台的第三方和apache的非常多东西类似,仅仅是用于Android上 我在项目里用的是这个 https://github.com/loopj/android-async-ht ...

  3. android中用get和post方式向服务器提交请求

    通过get和post方式向服务器发送请求首先说一下get和post的区别get请求方式是将提交的参数拼接在url地址后面,例如http://www.baidu.com/index.jsp?num=23 ...

  4. android客户端向服务器发送请求中文乱码的问

    android客户端向服务器发送请求的时候,并将参数保存到数据库时遇到了中文乱码的问题: 解决方法: url = "http://xxxx.com/Orders/saveorder.html ...

  5. 讨论HTTP POST 提交数据的几种方式

    转自:http://www.cnblogs.com/softidea/p/5745369.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PU ...

  6. C#中Post请求的两种方式发送参数链和Body的

    POST请求 有两种方式 一种是组装key=value这种参数对的方式 一种是直接把一个字符串发送过去 作为body的方式 我们在postman中可以看到 sfdsafd sdfsdfds publi ...

  7. 第二节:SSL证书的申请、配置(IIS通用)及跳转Https请求的两种方式

    一. 相关概念介绍 1. SSL证书服务 SSL证书服务由"服务商"联合多家国内外数字证书管理和颁发的权威机构.在xx云平台上直接提供的服务器数字证书.您可以在阿里云.腾讯云等平台 ...

  8. 怎样在Android开发中FPS游戏实现的两种方式比较

    怎样在Android开发中FPS游戏实现的两种方式比较 如何用Android平台开发FPS游戏,其实现过程有哪些方法,这些方法又有哪些不同的地方呢?首先让我们先了解下什么是FPS 英文名:FPS (F ...

  9. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

随机推荐

  1. python中打印文件名,行号,路径

    print "I have a proble! And here is at Line: %s"%sys._getframe().f_lineno PDB,哈哈http://doc ...

  2. 清华集训2014 day2 task3 矩阵变换

    题目 算法 稳定婚姻系统(其实就是贪心) 一个方案不合法,当且仅当下面这种情况: 设第\(i\)行选了数字\(x\),如果第\(j\)行有一个\(x\)在第\(i\)行的\(x\)后面,并且第\(j\ ...

  3. swift 自定义TabBarItem

    1.效果图     2.NewsViewController.swift // // NewsViewController.swift // NavigationDemo // // Created ...

  4. paip.按键替换映射总结

    paip.按键替换映射总结 作者Attilax ,  EMAIL:1466519819@qq.com  来源:attilax的专栏 地址:http://blog.csdn.net/attilax 因为 ...

  5. 【Cocos2d-x游戏引擎开发笔记(25)】XML解析

    原创文章,转载请注明出处:http://blog.csdn.net/zhy_cheng/article/details/9128819 XML是一种非常重要的文件格式,由于C++对XML的支持非常完善 ...

  6. Ubuntu14.04更新源

    Ubuntu14.04更新源 http://jingyan.baidu.com/article/7f41ecec1b7a2e593d095ce6.html Ubuntu源 http://wiki.ub ...

  7. Bee Framework_百度百科

    Bee Framework_百度百科 Bee Framework 编辑   目录 1详细信息 简介 特性 2工作 主要模块 编译要求 运行要求 目录结构 运行例程 安装步骤     1详细信息 简介 ...

  8. addEventListener()及attachEvent()区别分析

    Javascript 的addEventListener()及attachEvent()区别分析 Mozilla中: addEventListener的使用方式: target.addEventLis ...

  9. PHP判断远程文件是否存在的几种方法

    在做一个图片预览中图的东西,遇到一个问题,就是要判断远程文件是否存在(不是同一台服务器). 代码如下: 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 ...

  10. 设计模式模式适配器(Adapter)摘录

    23种子GOF设计模式一般分为三类:创建模式.结构模型.行为模式. 创建模式抽象的实例,他们帮助建立一个系统,是独立于如何.这是一个这些对象和陈述的组合.创建使用继承一个类架构更改实例,一个对象类型模 ...