HttpURLConnection GET和POST请求示例
2016-05-23 20:21
567 查看
1.GET:
2.POST
String _path = "http://*:8080/hpwy/token?appId=1234&secret=1234"; URL _url = new URL(_path); HttpURLConnection _conn = (HttpURLConnection) _url.openConnection(); InputStream _input; byte[] _buffer = new byte[1024]; long _length = 0; String _s; _conn.setConnectTimeout(5 * 1000); _conn.setRequestMethod("GET"); _conn.setDoInput(true); _input = _conn.getInputStream(); _length = _input.read(_buffer); _s = new String(_buffer,0, (int)_length);
2.POST
if (null == _token || _token.length() <= 0) return; String _path = "http://*:8080/hpwy/login/phone"; String _stringEntity; String _resultContent; int _length; byte[] _byteEntity; byte[] _buffer = new byte[1024]; JSONObject _jsonEntity; OutputStream _outputStream; InputStream _inputStream; _jsonEntity = new JSONObject(); _jsonEntity.put("phone", "1591234567"); _jsonEntity.put("token", _token); _stringEntity = _jsonEntity.toString(); _byteEntity = _stringEntity.getBytes(Charset.forName("UTF-8")); URL _url = new URL(_path); HttpURLConnection _conn = (HttpURLConnection) _url.openConnection(); _conn.setConnectTimeout(5 * 1000); _conn.setRequestMethod("POST"); _conn.setRequestProperty("accept", "*/*"); _conn.setRequestProperty("connection", "Keep-Alive"); _conn.setRequestProperty("Content-Type", "application/json"); _conn.setDoInput(true); _conn.setDoOutput(true); _outputStream = _conn.getOutputStream(); _outputStream.write(_byteEntity); _outputStream.flush(); _inputStream = _conn.getInputStream(); _length = _inputStream.read(_buffer); _resultContent = new String(_buffer, 0, _length); Log.d("hpwy", _resultContent);
相关文章推荐
- GET和post取值限制区别分析
- 深入C#中get与set的详解
- POST与GET方法的区别简要分析
- C#使用post发送和接收数据的方法
- PHP响应post请求上传文件的方法
- VB使用XMLHTTP实现Post与Get的方法
- 分享下GET和POST的真正区别
- php采用ajax数据提交post与post常见方法总结
- ajax 的post方法实例(带循环)
- C#实现的三种模拟自动登录和提交POST信息的方法
- ASP.Net Post方式获取数据流的一种简单写法
- C#中在WebClient中使用post发送数据实现方法
- ASP.NET的HtmlForm控件学习及Post与Get的区别概述
- asp教程中get post提交表单有5点区别
- jQuery用于请求服务器的函数
- AJAX的post请求与上传文件
- 半自动添加Grafana 模板之 ---- POST提交
- python post raw
- PHP模拟POST请求