ITEEDU

编程学习网


  • 首页

  • 标签

  • 分类

  • 归档

  • 关于

  • 旧站归档

  • 搜索

反向代理之http请求分析

发表于 2017-03-07 更新于 2019-06-29 分类于 架构 , 反向代理

主要分析http请求内容,分析对象:

HttpServletRequest request

取出所有请求头和参数:

Map<String, String> headers=new HashMap<String, String>();
for (Enumeration<String> e = request.getHeaderNames(); e
        .hasMoreElements();) {
    String name = e.nextElement().toString();
    headers.put(name, request.getHeader(name));
}
Map<String, String> param=new HashMap<String, String>();
for(Enumeration<String> e=request.getParameterNames();e
        .hasMoreElements();){
    String name = e.nextElement().toString();
    param.put(name, request.getParameter(name));
}

取出所有请求体:

byte[] data=IOUtils.readBytesFromStream(request.getInputStream());
String strData=new String(data,"utf-8");

取参数request.getParameter(name)和取请求体IOUtils.readBytesFromStream(request.getInputStream())不能同时用,因为两个操作都会读InputStream,但InputStream只能读一次,第二次读返回为空。

但IOUtils.readBytesFromStream(request.getInputStream())不影响请求头的读取。

普通请求代码

String strUrl = "http://localhost:8080/front_new/rproxy/123/456";
Map<String, String> requestParams = new HashMap<String, String>();
Map<String, Object> head = new HashMap<String, Object>();
head.put("headkey1", "headkey1value");
String jsonHead = JSONObject.toJSONString(head);
Map<String, Object> content = new HashMap<String, Object>();
content.put("contentKey1", "contentKey1value");
content.put("contentKey2", "contentKey3value");
String jsonContent = JSONObject.toJSONString(content);
requestParams.put("jsonHead", jsonHead);
requestParams.put("jsonContent", jsonContent);
PostMethod postMethod = null;
HttpClient httpClient = new HttpClient();
// 设置超时时间
httpClient.getHttpConnectionManager().getParams()
        .setConnectionTimeout(30000);
httpClient.getHttpConnectionManager().getParams().setSoTimeout(30000);
postMethod = new PostMethod(strUrl);
int i = 0;
NameValuePair[] nvps = new NameValuePair[requestParams.size()];
for (String strKey : requestParams.keySet()) {
    NameValuePair nvp = new NameValuePair();
    nvp.setName(strKey);
    nvp.setValue(requestParams.get(strKey));
    nvps[i] = nvp;
    i++;
}
postMethod.setRequestBody(nvps);
httpClient.executeMethod(postMethod);
String resultStr = postMethod.getResponseBodyAsString();
System.err.println(resultStr);

请求头:

{Host=localhost:8080,
Content-Length=157,
User-Agent=Jakarta Commons-HttpClient/3.1,
Content-Type=application/x-www-form-urlencoded}

请求内容:

{ jsonHead ={ "headkey1" : "headkey1value" },
 jsonContent ={ "contentKey1" : "contentKey1value",
 "contentKey2" : "contentKey3value" }}

请求流内容:

jsonHead=%7B%22headkey1%22%3A%22headkey1value%22%7D&jsonContent=%7B%22contentKey1%22%3A%22contentKey1value%22%2C%22contentKey2%22%3A%22contentKey3value%22%7D

文件上传

DefaultHttpClient httpclient = new DefaultHttpClient();
// 设置超时时间
HttpPost httppost = new HttpPost(
        "http://localhost:8080/front_new/rproxy/123/456");
FileBody bin = new FileBody(new File("F:/aaa.txt"));
FileBody bin2 = new FileBody(new File("F:/bbb.txt"));
StringBody comment = new StringBody("adfadsfasdf");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("file1", bin);// file1为请求后台的File upload;属性
reqEntity.addPart("file2", bin2);// file2为请求后台的File upload;属性
reqEntity.addPart("filename1", comment);// filename1为请求后台的普通参数;属性
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);

header:

{Host=localhost:8080,
Content-Length=686,
User-Agent=Apache-HttpClient/4.5.2 (Java/1.7.0_67),
Connection=keep-alive,
Content-Type=multipart/form-data;
boundary=uj-cDkEsr2eER-jIpm2EaVBmVST_Mtte1AJR;
charset=US-ASCII}

请求参数为空。

请求体为:

--aKJdjfxWzYZm2BpDBoDCod15aGKE0a_r96-V
Content-Disposition: form-data; name="file1"; filename="aaa.txt"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

 这是测试文件。
这是测试文件。
这是测试文件。
--aKJdjfxWzYZm2BpDBoDCod15aGKE0a_r96-V
Content-Disposition: form-data; name="file2"; filename="bbb.txt"
Content-Type: application/octet-stream
Content-Transfer-Encoding: binary

 这是测试文件。
这是测试文件。
这是测试文件。
--aKJdjfxWzYZm2BpDBoDCod15aGKE0a_r96-V
Content-Disposition: form-data; name="filename1"
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 8bit

adfadsfasdf
--aKJdjfxWzYZm2BpDBoDCod15aGKE0a_r96-V--

两个文件都为utf-8编码,内容都为

这是测试文件。
这是测试文件。
这是测试文件。

Content-Type

普通请求为

application/x-www-form-urlencoded

一般的表示提交,数据为key=value形式数据。

可以用request.getParameter(name)取出参数。

文件上传为

multipart/form-data

不能用request.getParameter(name)取出参数。

证心 微信支付

微信支付

# HttpClient
第9章建立单元测试框架-实用Common Lisp编程笔记
反向代理之http请求乱码分析
  • 文章目录
  • 站点概览

证心

367 日志
45 分类
78 标签
GitHub 微博
  1. 1. 普通请求代码
  2. 2. 文件上传
  3. 3. Content-Type
京ICP备16069454号 © 2020 证心