这两天在做http服务端请求操作,客户端post数据到服务端后,服务端通过request.getParameter()进行请求,无法读取到数据,搜索了一下发现是因为设置为text/plain模式才导致读取不到数据
urlConn.setRequestProperty("Content-Type","text/plain; charset=utf-8");
若设置为以下方式,则通过request.getParameter()可以读取到数据
urlConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
- enctype的三种编码
form表单中enctype属性可以用来控制对表单数据的发送前的如何进行编码,即在发送到服务器之前,所有字符都会进行编码(空格转换为 "+" 加号,特殊符号转换为 ASCII HEX 值)。默认是application/x-www-form-urlencoded。
multipart/form-data用于发送二进制的文件,其他两种类型不能用于发送文件
text/plain用于发送纯文本内容,不对特殊字符进行编码,一般用于email之类的。
application/x-www-form-urlencoded和text/plain的区别简单讲就是一个发送html内容,一个发送纯文本内容
- application/x-www-form-urlencoded 在发送前编码所有字符(默认)
- multipart/form-data 不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。
- text/plain 空格转换为 "+" 加号,但不对特殊字符编码。
- 当定义enctype为application/x-www-form-urlencoded时,使用以下方式接收数据
request.getParameter(参数名);
- 当定义enctype为text/plain时,使用以下方式接收数据
// 接收请求数据 BufferedReader reader = request.getReader(); char[] buf = new char[512]; int len = 0; StringBuffer contentBuffer = new StringBuffer(); while ((len = reader.read(buf)) != -1) { contentBuffer.append(buf, 0, len); } String content = contentBuffer.toString(); if(content == null){ content = ""; }
- post 与get
post和get可以通过键值对的方式进行参数传输,服务端通过request.getparameter方式进行请求获取数据。
- 客户端post数据到服务端,服务端接收处理
public class UrlConnection { @SuppressWarnings("finally") public static boolean response(String url,String content) { String line = ""; String message = ""; String returnData = ""; boolean postState = false; BufferedReader bufferedReader = null; try { URL urlObject = new URL(url); HttpURLConnection urlConn = (HttpURLConnection) urlObject.openConnection(); urlConn.setDoOutput(true); /*设定禁用缓存*/ urlConn.setRequestProperty("Pragma:", "no-cache"); urlConn.setRequestProperty("Cache-Control", "no-cache"); /*维持长连接*/ urlConn.setRequestProperty("Connection", "Keep-Alive"); /*设置字符集*/ urlConn.setRequestProperty("Charset", "UTF-8"); /*设定输出格式为json*/ urlConn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); /*设置使用POST的方式发送*/ urlConn.setRequestMethod("POST"); /*设置不使用缓存*/ urlConn.setUseCaches(false); /*设置容许输出*/ urlConn.setDoOutput(true); /*设置容许输入*/ urlConn.setDoInput(true); urlConn.connect(); OutputStreamWriter outStreamWriter = new OutputStreamWriter(urlConn.getOutputStream(),"UTF-8"); outStreamWriter.write(content); outStreamWriter.flush(); outStreamWriter.close(); /*若post失败*/ if((urlConn.getResponseCode() != 200)){ returnData = "{"jsonStrStatus":0,"processResults":[]}"; message = "发送POST失败!"+ "code="+urlConn.getResponseCode() + "," + "失败消息:"+ urlConn.getResponseMessage(); // 定义BufferedReader输入流来读取URL的响应 InputStream errorStream = urlConn.getErrorStream(); if(errorStream != null) { InputStreamReader inputStreamReader = new InputStreamReader(errorStream,"utf-8"); bufferedReader = new BufferedReader(inputStreamReader); while ((line = bufferedReader.readLine()) != null) { message += line; } inputStreamReader.close(); } errorStream.close(); System.out.println("发送失败!错误信息为:"+message); }else{ /*发送成功返回发送成功状态*/ postState = true; // 定义BufferedReader输入流来读取URL的响应 InputStream inputStream = urlConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8"); bufferedReader = new BufferedReader(inputStreamReader); while ((line = bufferedReader.readLine()) != null) { message += line; } returnData = message; inputStream.close(); inputStreamReader.close(); System.out.println("发送POST成功!返回内容为:" + message); } } catch (Exception e) { e.printStackTrace(); }finally{ try { if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException ex) { ex.printStackTrace(); } return postState; } } /*读取request数据*/ public static String getRequestData(HttpServletRequest request) throws IOException{ BufferedReader reader = request.getReader(); char[] buf = new char[512]; int len = 0; StringBuffer contentBuffer = new StringBuffer(); while ((len = reader.read(buf)) != -1) { contentBuffer.append(buf, 0, len); } String content = contentBuffer.toString(); if(content == null){ content = ""; } return content; } }