HttpURLConnection post set params 设置请求参数的三种方法
来源:互联网 发布:linux解压tar命令 编辑:程序博客网 时间:2024/05/17 08:15
/** * set params one way OutputStream */ byte[] bytesParams = paramsStr.getBytes(); // 发送请求params参数 connection.getOutputStream().write(bytesParams); /** * set params two way PrintWriter */ PrintWriter printWriter = new PrintWriter(connection.getOutputStream()); //PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8")); // 发送请求params参数 printWriter.write(paramsStr); printWriter.flush(); /** * set params three way OutputStreamWriter */ OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream(), "UTF-8"); // 发送请求params参数 out.write(paramsStr); out.flush();
demo:
/** * @param pathurl * @param paramsStr * @return */ private static String postUrlBackStr(String pathurl, String paramsStr) { String backStr = ""; InputStream inputStream = null; ByteArrayOutputStream baos = null; try { URL url = new URL(pathurl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设定请求的方法为"POST",默认是GET connection.setRequestMethod("POST"); connection.setConnectTimeout(50000); connection.setReadTimeout(50000); // User-Agent IE9的标识 connection.setRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"); connection.setRequestProperty("Accept-Language", "zh-CN"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Charset", "UTF-8"); /** * 当我们要获取我们请求的http地址访问的数据时就是使用connection.getInputStream().read()方式时我们就需要setDoInput(true), 根据api文档我们可知doInput默认就是为true。我们可以不用手动设置了,如果不需要读取输入流的话那就setDoInput(false)。 当我们要采用非get请求给一个http网络地址传参 就是使用connection.getOutputStream().write() 方法时我们就需要setDoOutput(true), 默认是false */ // 设置是否从httpUrlConnection读入,默认情况下是true; connection.setDoInput(true); // 设置是否向httpUrlConnection输出,如果是post请求,参数要放在http正文内,因此需要设为true, 默认是false; connection.setDoOutput(true); connection.setUseCaches(false); /** * set params one way OutputStream */ /* byte[] bytesParams = paramsStr.getBytes(); // 发送请求params参数 connection.getOutputStream().write(bytesParams);*/ /** * set params two way PrintWriter */ /* PrintWriter printWriter = new PrintWriter(connection.getOutputStream()); //PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"UTF-8")); // 发送请求params参数 printWriter.write(paramsStr); printWriter.flush();*/ /** * set params three way OutputStreamWriter */ OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream(), "UTF-8"); // 发送请求params参数 out.write(paramsStr); out.flush(); connection.connect();// int contentLength = connection.getContentLength(); if (connection.getResponseCode() == 200) { inputStream = connection.getInputStream();//会隐式调用connect() baos = new ByteArrayOutputStream(); int readLen; byte[] bytes = new byte[1024]; while ((readLen = inputStream.read(bytes)) != -1) { baos.write(bytes, 0, readLen); } backStr = baos.toString(); Log.i(TAG, "backStr:" + backStr); } else { Log.e(TAG, "请求失败 code:" + connection.getResponseCode()); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (baos != null) { baos.close(); } if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } return backStr; }
0 0
- HttpURLConnection post set params 设置请求参数的三种方法
- HttpUrlConnection的get和post请求方法
- HttpURLConnection的POST请求
- HttpUrlConnection的post请求
- HttpURLConnection--HttpURLConnection的Post请求方式
- HttpURLConnection 的 POST 方法
- HttpURLConnection的post请求总结
- HttpURLConnection发出的post请求
- HttpURLConnection发出的post请求
- HttpURLConnection的Post请求方式
- nodejs 之req.body req.query req.params 三种获取参数的方法
- HttpUrlConnection的GET请求和POST请求
- httpurlconnection的Get请求和Post请求
- 安卓中HttpUrlConnection get和post请求方法
- post请求xml参数的方法
- php发送post请求的三种方法
- php发送post请求的三种方法
- php发送post请求的三种方法
- leetcode 389. Find the Difference
- 第13周项目3-Dijkstra算法的验证
- Recyclerview
- 1123 练习
- 粒子群算法(1)----粒子群算法简介
- HttpURLConnection post set params 设置请求参数的三种方法
- Qt实现基本QMainWindow主窗口程序
- Eclipse 自动生成注释信息
- HDU2042_不容易系列之二
- Java并发编程与技术内幕:聊聊锁的技术内幕(上)
- angularJs入门之购物车实现
- 选择排序
- 如何设计Lighthoused定位接收电路
- unity 学习笔记(改变物体旋转中心)