java POST请求body传参
来源:互联网 发布:filezilla是什么软件 编辑:程序博客网 时间:2024/05/21 11:14
public String post(String actionUrl, Map<String, String> params) throws IOException { String BOUNDARY = java.util.UUID.randomUUID().toString(); String PREFIX = "--"; String LINEND = "\r\n"; String MULTIPART_FROM_DATA = "multipart/form-data"; String CHARSET = "UTF-8"; URL uri = new URL(actionUrl); HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(30 * 1000); // 缓存的最长时间 conn.setDoInput(true);// 允许输入 conn.setDoOutput(true);// 允许输出 conn.setUseCaches(false); // 不允许使用缓存 conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY); StringBuilder sb = new StringBuilder(); if (params!=null) { // 首先组拼文本类型的参数 for (Map.Entry<String, String> entry : params.entrySet()) { sb.append(PREFIX); sb.append(BOUNDARY); sb.append(LINEND); sb.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"" + LINEND); sb.append("Content-Type: text/plain; charset=" + CHARSET + LINEND); sb.append("Content-Transfer-Encoding: 8bit" + LINEND); sb.append(LINEND); sb.append(entry.getValue()); sb.append(LINEND); } } DataOutputStream outStream = new DataOutputStream( conn.getOutputStream()); if (!TextUtils.isEmpty(sb.toString())) { outStream.write(sb.toString().getBytes()); } // 请求结束标志 byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); outStream.flush(); // 得到响应码 int res = conn.getResponseCode(); InputStream in = conn.getInputStream(); if (res == 200) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in, "UTF-8")); StringBuffer buffer = new StringBuffer(); String line = ""; while ((line = bufferedReader.readLine()) != null){ buffer.append(line); }// int ch;// StringBuilder sb2 = new StringBuilder();// while ((ch = in.read()) != -1) {// sb2.append((char) ch);// } return buffer.toString(); } outStream.close(); conn.disconnect(); return in.toString(); }
阅读全文
0 0
- java POST请求body传参
- java post请求设置body中文乱码问题
- java通过HttpServletRequest获取post请求中的body内容
- Http Post请求 带Header和Body
- Http post请求 entity body的格式
- gin 获取post请求的json body
- AFNetWorking Post请求,请求参数放在Body处
- 关于POST请求在body中的数据格式问题
- express中间件body-parser实现拿到post请求的数据
- servlet 获取 body Json数据(post 请求)
- 获得WebAPI原始POST请求BODY的JSON内容
- Express配置post请求的body解析
- java读取http请求中的body
- java读取http请求中的body
- java读取http请求中的body
- java 流获取请求的body参数
- java net post 请求
- java post请求接口
- c语言关闭正在运行的应用程序
- 用Python和Pygame写游戏-从入门到精通(7) 混杂的例子
- 转自博客园- 林梦然+++opencv下指定文件夹下的图片灰度化(图片的读取与保存)-------简单记录
- maven项目分模块
- 6249856.html
- java POST请求body传参
- Ubuntu16.04环境变量设置
- 我和你
- 罪犯转移
- IDEA安装码云插件
- 神通广大的JS
- ECO论文翻译:Efficient Convolution Operators for Tracking论文翻译
- C语言中auto,register,static,const,volatile的区别详细解析
- ps备忘2