象这样只能得到请求体,不能获取到包括头部的整个二进制数据
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
OutputStreamWriter osw = new OutputStreamWriter(resp.getOutputStream());
InputStream is = req.getInputStream();
osw.write(Constant.TEST_WEBSOCKET_PAGE);
osw.flush();
}
处理 Http 是 Web 容器的工作,AJP 将数据传给 Servlet 就已经是解析过的对象,因此不能直接获取到原本的内容
自己再拼接回去咯 😆
GET http://www.ruanyifeng.com/static/themes/theme_scrapbook/theme_scrapbook.css HTTP/1.1
Host: www.ruanyifeng.com
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
Accept: text/css,*/*;q=0.1
Referer: http://www.ruanyifeng.com/blog/2016/08/http.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: zh-CN,zh;q=0.8
Cookie: _ga=GA1.2.1640937992.1474458063
public static final byte[] PROTOCOL_BYTES = " HTTP/1.1\r\n".getBytes();
public static final byte[] CRLF_BYTES = "\r\n".getBytes();
public static final byte BLANK_BYTE = ' ';
public static final byte[] COLON_BYTES = ": ".getBytes();
public static final byte QUESTION_BYTE = '?';
public byte[] getRequestRaw(HttpServletRequest req) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
// header values
bufferWriteBytesFromStringAllowNull(buffer, req.getMethod());
buffer.write(BLANK_BYTE);
bufferWriteBytesFromStringAllowNull(buffer, req.getRequestURL().toString());
buffer.write(QUESTION_BYTE);
bufferWriteBytesFromStringAllowNull(buffer, req.getQueryString());
buffer.write(PROTOCOL_BYTES);
Enumeration<String> headerNames = req.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = req.getHeader(headerName);
buffer.write(headerName.getBytes());
buffer.write(COLON_BYTES);
bufferWriteBytesFromStringAllowNull(buffer, headerValue);
buffer.write(CRLF_BYTES);
}
buffer.write(CRLF_BYTES);
// body values
InputStream inputStream = req.getInputStream();
int nRead;
byte[] data = new byte[1024];
while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
byte[] byteArray = buffer.toByteArray();
return byteArray;
}
public String getRequestBase64(HttpServletRequest req) throws IOException {
byte[] rawBytes = getRequestRaw(req);
return Base64.getEncoder().encodeToString(rawBytes);
}
public String encodeBase64BytesString(String base64Bytes) {
byte[] bytes = Base64.getDecoder().decode(base64Bytes);
StringBuffer stringBuffer = new StringBuffer();
for (byte b : bytes) {
stringBuffer.append((char) b);
}
return stringBuffer.toString();
}
public void bufferWriteBytesFromStringAllowNull(ByteArrayOutputStream byteArrayOutputStream, String string) throws IOException {
if (string != null) {
if (string.getBytes() != null) {
byteArrayOutputStream.write(string.getBytes());
}
}
}
那个 ByteArrayOutputStream 应该可以共用,需要吧所有的数据都给转到 byte[]
问一下那个 bufferWriteBytesFromStringAllowNull 应该是是不是写成继承扩展比较好,而不是调用自身
消息同步不会写,想想还是算了。。。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于