Android面向HTTP协议发送post请求
/**
* 採用post请求的方式
*
* @param username
* @param password
* @return null表示求得的路径有问题,text返回请求得到的数据
*/
public static String postRequest(String username, String password) {
try {
String path = "http://172.22.64.156:8080/0001AndroidWebService/LoginServlet";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(500);
conn.setRequestMethod("POST");
// username=donghongyu&&password=123
// 准备要传输的数据
String data = "username=" + URLEncoder.encode(username)
+ "&password=" + URLEncoder.encode(password);
// 设置请求的内容的类型
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", data.length() + ""); // 开启向server写入的权限
conn.setDefaultUseCaches(true);
// 获取http连接的输出流
OutputStream os = conn.getOutputStream();
// 向server写入数据
os.write(data.getBytes()); int code = conn.getResponseCode();
if (code == 200) {
// 请求成功
InputStream is = conn.getInputStream();
String text = StreamUtil.readStream(is);
return text;
} else {
// 请求失败
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Android面向HTTP协议发送post请求的更多相关文章
- Android面向HTTP协议发送get请求
/** * 採用get请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ pub ...
- 在android用Get方式发送http请求
烦人的日子终于过去啦,终于又可以写博客啦,对自己的android学习做个总结,方便以后查看...... 一.在android用Get方式发送http请求,使用的是java标准类,也比较简单. 主要分以 ...
- 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求
一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议,是 ...
- php模拟HTTP协议发送post请求方法
今天用到php模拟http发送post请求记录 代码如下: <?php $url = 'xxxx.com'; $data = 'a=one&b=two'; $data = urlenco ...
- Android 的OkHttp(发送网络请求)
今天讲的是和HttpURLConnection差不多的OkHttp; 先把网站献上: 官网介绍okhttp的: https://square.github.io/okhttp/ 下载postman的: ...
- http协议发送post请求
package post; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamR ...
- android中Post方式发送HTTP请求
Post方式比Get方式要复杂一点,因为该方式需要将请求的参数放在http请求的正文中,所以需要构造请求体. 步骤: 1.构造URL URL url = new URL(PATH); 2.设置连接 H ...
- Android面HTTP协议发送get要求
/** * 采纳get办法要求 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ publ ...
- Android中使用HttpClient发送Get请求
这里要指定编码,不然服务器接收到的会是乱码的.
随机推荐
- zzuli oj 1145 有问题的里程表 2
Description 某辆汽车有一个里程表,该里程表可以显示一个整数,为该车走过的公里数.然而这个里程表有个毛病:它总是从3变到5,而跳过数字4,里程表所有位(个位. 十位.百位等)上的数字都是如此 ...
- usb host和usb device
S3C2440的数据手册将USB功能分为两章--usb host和usb device.具体什么意思呢? usb host: 微处理器作为usb主设备,可以挂接U盘之类的从属设备. usb devic ...
- 文件操作 - NSFileManager
iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容.iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内.默认 ...
- Delphi XE5 Device compatibility
Delphi XE5 Device compatibility https://docs.google.com/spreadsheet/ccc?key=0AoEN2CEsVvJ0dGhVaWJE ...
- The Promise of Deep Learning
The Promise of Deep Learning By Yoshua Bengio Humans have long dreamed of creating machines that thi ...
- ABAP写的一个递归
需求:计算下面树形结构中每个子节点与最上层父节点的对应关系. DATA:BEGIN OF lt_ztab OCCURS 0, a TYPE string, b TYPE str ...
- Stanford CoreNLP--功能列表
Standford CoreNLP包含很多功能,github上有源码,github地址:Stanford CoreNLP,有需要的话可以下载看看. 主要内容在网站上都有描述,原文是这样写的: Choo ...
- HTML Jquery;marquee标签
在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...
- JBossESB教程(一)——开发环境的搭建
前言 上篇对SOA的概述里面,在说SOA构建需要考虑什么时,提到了ESB,它作为SOA的基础设施而存在. 从这篇开始,将对ESB的其中一个实现JBossESB进行一个从头开始的讲解,既然是从头开始,那 ...
- 【HDOJ】1239 Calling Extraterrestrial Intelligence Again
这题wa了很多词,题目本身很简单,把a/b搞反了,半天才检查出来. #include <stdio.h> #include <string.h> #include <ma ...