/**
* 採用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请求的更多相关文章

  1. Android面向HTTP协议发送get请求

    /** * 採用get请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ pub ...

  2. 在android用Get方式发送http请求

    烦人的日子终于过去啦,终于又可以写博客啦,对自己的android学习做个总结,方便以后查看...... 一.在android用Get方式发送http请求,使用的是java标准类,也比较简单. 主要分以 ...

  3. 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求

    一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议,是 ...

  4. php模拟HTTP协议发送post请求方法

    今天用到php模拟http发送post请求记录 代码如下: <?php $url = 'xxxx.com'; $data = 'a=one&b=two'; $data = urlenco ...

  5. Android 的OkHttp(发送网络请求)

    今天讲的是和HttpURLConnection差不多的OkHttp; 先把网站献上: 官网介绍okhttp的: https://square.github.io/okhttp/ 下载postman的: ...

  6. http协议发送post请求

    package post; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamR ...

  7. android中Post方式发送HTTP请求

    Post方式比Get方式要复杂一点,因为该方式需要将请求的参数放在http请求的正文中,所以需要构造请求体. 步骤: 1.构造URL URL url = new URL(PATH); 2.设置连接 H ...

  8. Android面HTTP协议发送get要求

    /** * 采纳get办法要求 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ publ ...

  9. Android中使用HttpClient发送Get请求

    这里要指定编码,不然服务器接收到的会是乱码的.

随机推荐

  1. C#黑白棋制作~

    前些天自己复习一下C#语言 做了个黑白棋,望大家看一下,可能有些bug嘿嘿 链接如下 http://files.cnblogs.com/files/flyingjun/%E9%BB%91%E7%99% ...

  2. Java 之 网络编程

    1.OSI模型 a.全称:开放系统互联 b.七层模型:应用层.表示层.会话层.传输层.网络层.数据链路层.物理层 c.注意:OSI模型 是所谓的 理论标准 2.TCP/IP模型 a.四层模型:应用层. ...

  3. leetcode 组合题

    1.Subsets 代码1: class Solution { public: vector<vector<int> > subsets(vector<int> & ...

  4. Delphi组件indy 10中IdTCPServer修正及SSL使用心得

    indy 10终于随着Delphi2005发布了,不过indy套件在我的印象中总是复杂并且BUG不断,说实话,不是看在他一整套组件的面子上,我还是喜欢VCL原生的Socket组件,简洁,清晰.Indy ...

  5. 看了一下安装文件. 是qt4python 下用了 webkit,包装了bootstrap

    Pg9.6 安装包里的pgadmin4 反正软件是开源的,慢慢看源码呗.

  6. Android listview.item.clear()与listview.clear()的区别

    listview.clear()与listview.item.clear()的区别就是使用了listview.item.clear()后,listview控件中仍然保存着listviewitem项的结 ...

  7. 一台机器上运行多个ActiveMq

    由于业务需要一台机器上运行多个ActiveMq,这里主要说一下有什么地方不重复: 1.brokerName名称不能重复 2.端口号不能重复uri = tcp://localhost:50509 3.k ...

  8. C#的装箱和拆箱

    1:装箱 其实就是将一个值类型的转换成一个引用类型,或者把值类型转换成一个被该值类型那个应用的接口类型,这个过程会使用堆栈.被装箱的值是作为一个复制赋给对象的. int  intValue = 100 ...

  9. Linux命令行及Vim简单学习记录

    Linux命令行 1.打开命令行 Ctrl+Alt+t 2.目录 显示当前目录的文件列表 ls 跳转至当前目录中的x文件夹 cd x 返回根目录 cd 3.文件 新建文件1.cpp touch ./1 ...

  10. [ZOJ 3622] Magic Number

    Magic Number Time Limit: 2 Seconds      Memory Limit: 32768 KB A positive number y is called magic n ...