所有HTTP请求参数及报文查看SERVLET

时间:2022-10-17 17:44:12

HttpRequestServlet.java

说明:

用于接受所有http形式的请求,并把接受到的request中param及getInputStream全打印出来.

package king.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.UUID; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* 用于接受所有http形式的请求,并把接受到的request中param及getInputStream全打印出来
* @author King
*
*/
public class HttpRequestServlet extends HttpServlet{
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
System.err.println("我是服务器,通过request.getParameter()获取的参数:");
StringBuilder paramSB = new StringBuilder();
Enumeration<?> paramNames=request.getParameterNames();
while(paramNames.hasMoreElements()){
String name=(String)paramNames.nextElement();
String valueNotDecode=request.getParameter(name);
String valueDecode=new String(valueNotDecode.getBytes("UTF-8"),"UTF-8");
paramSB.append(name + "==" + valueDecode+"\n");
}
System.out.println("我是服务器,通过param读取的参数如下:\n"+paramSB.toString());
PrintWriter writer = response.getWriter();
writer.write("通过param读取的参数如下:\n"+paramSB.toString());
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
} @Override
public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
// 如果"您的文本内容" 用UTF-8编码后,再以conn.setRequestProperty("content-type", "text/xml;charset=iso-8859-1");发送到本服务端,那么需要对参数内容进行如下解码
// String name = new String(request.getParameter("name").getBytes("iso-8859-1"),"UTF-8");
// System.out.println("name : "+ name); System.err.println("我是服务器,通过request.getParameter()获取的参数:");
StringBuilder paramSB = new StringBuilder();
Enumeration<?> paramNames=request.getParameterNames();
while(paramNames.hasMoreElements()){
String name=(String)paramNames.nextElement();
String valueNotDecode=request.getParameter(name);
String valueDecode=new String(valueNotDecode.getBytes("iso-8859-1"),"UTF-8");
System.out.println(name + "==" + valueDecode);
paramSB.append(name + "==" + valueDecode+"\n");
} //使用apache的httpcomponents 的post请求时:formparams.add(new BasicNameValuePair("key","value")); 也只能从这里获取到!
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
String line = null;
StringBuilder streamSB = new StringBuilder();
while ((line = br.readLine()) != null) {
streamSB.append(line);
}
String decode = URLDecoder.decode(streamSB.toString(), "UTF-8");
System.out.println("我是服务器,通过request.getInputStream()读取的参数:\n" + decode); response.setCharacterEncoding("UTF-8");//第一优先级高于第二优先级,只能用来设置out输出流中所采用的编码
// response.setContentType("text/html;charset=UTF-8");//第二优先级,不仅能用来设置out输出流中所采用的编码,也可以设置浏览器接收到这些字符后以什么编码方式来解码
PrintWriter writer = response.getWriter();
writer.write("通过param读取的参数如下:\n"+paramSB.toString());
writer.write("通过stream读取的参数如下:\n"+decode);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 客户端通过conn.setRequestProperty(key,value),
* 即可在服务端通过request.getHeader(key)获取到对应的head协议头值
* @param request
* @param response
* @throws Exception
*/
public void tip(HttpServletRequest request, HttpServletResponse response) throws Exception{
//在客户端用
HttpURLConnection conn = (HttpURLConnection) new URL("http://localhost:8080/httpserver").openConnection();
conn.setRequestProperty("Accept", "application/soap+xml, application/dime, multipart/related, text/*");
//就能在本服务端用
request.getHeader("Accept");//获取到Head协议头中的值.
}
}