HttpURLConnection网络获取资源之:POST方法
来源:互联网 发布:锵锵三人行9月11 知乎 编辑:程序博客网 时间:2024/04/29 01:16
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/*
* post请求登录注意点:
*
* 原理:客户端通过输出流,将数据写给服务端,服务端才会给予响应。
*
* 注意点:
*
* 1.手动调用setDoOutput(true),打开连接中的输出流。
*
* 2.调用getOutputStream()--->OutputStream,将参数调用write,写给服务端。
*/
public class Demo05_Post_Login {
public static void main(String[] args) throws Exception {
String baseUrl = "http://10.0.158.239:8080/Day22_Server/LoginServlet";
String params = "username=admin&password=123";
URL url = new URL(baseUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方式为post
connection.setRequestMethod("POST");//注意,POST为大写,且为字符串
connection.setConnectTimeout(5000);//设置网络连接超时,可有可无
connection.setDoInput(true);// 打开连接中的输入流,用于读取服务端返回的数据。该方法的默认值就是true。
connection.setDoOutput(true);// 打开连接中的输出流,用于客户端向服务器写数据。该方法的默认值为false。
// 将参数,写给服务器端-->输出
OutputStream outputStream = connection.getOutputStream();
outputStream.write(params.getBytes());
// 获取响应码
if (connection.getResponseCode() == 200) {
InputStream inputStream = connection.getInputStream();
byte[] bs = new byte[24];
int len = 0;
len = inputStream.read(bs);
String result = new String(bs, 0, len);
System.out.println(result);
}
}
}
0 0
- HttpURLConnection网络获取资源之:POST方法
- 详解GET方法:HttpURLConnection从网络获取资源
- android网络编程之HttpUrlConnection--POST请求
- HttpURL网络获取资源之:GET方法
- Android HttpURLConnection获取网络下载图片POST请求方式
- HttpURLConnection 的 POST 方法
- HttpURLConnection-Post方法实现
- HttpClient之post方法和HttpUrlConnection之Get方法
- android网络编程之HttpUrlConnection的讲解--POST请求
- android网络编程之HttpUrlConnection的讲解--POST请求
- HttpURLConnection,get,post,网络请求
- HttpURLConnection 网络请求 get post
- HttpURLConnection获取网络图片
- HttpURLConnection网络获取
- 网络编程之POST方法
- Android HttpURLConnection(Get,Post方式)进行网络通信 获取数据和网络图片
- 第十二篇:Android GET请求获取网络数据以及POST数据(使用HttpURLConnection)
- 请求网络之HttpUrlConnection
- 自定义makefile
- Hive on Spark安装配置详解
- 位运算入门应用及技巧
- qt QTabWidget
- 随机数去重排序
- HttpURLConnection网络获取资源之:POST方法
- net start mysql 发生系统错误5
- NKOI 3747 宠物
- centralWidget中显示多个widget的实现与细节
- caffe学习笔记之——迭代次数的选取
- 支持跨域的html标签
- Myeclipse2015如何新建struts2的XML文件
- 买无人机之前你需要知道的几件事
- android读取assets文件示例