简体   繁体   English

使用Jakarta HttpClient模仿POST网络请求

[英]Using Jakarta HttpClient to mimic a POST network request

I try to mimic a network request send by a closed source application. 我尝试模仿封闭源应用程序发送的网络请求。 The following is the network request snapshot captured from the closed source application : 以下是从封闭源应用程序捕获的网络请求快照:

- Http: Request, POST /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage
    Command: POST
  - URI: /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage?
     Location: /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage
    ProtocolVersion: HTTP/1.1
  - ContentType:  application/x-www-form-urlencoded
     MediaType:  application/x-www-form-urlencoded
    Cache-Control:  no-cache
    Pragma:  no-cache
    UserAgent:  Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_06
    Host:  www.xxx.com:20000
    Accept:  text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection:  keep-alive
    ContentLength:  164
    HeaderEnd: CRLF
  - payload: HttpContentType =  application/x-www-form-urlencoded
     [SORT]: 0,1,0,10,5,0,KL,0
     [FIELD]: 33,38,51,58,68,88,78,98,99,101,56,57,69,70,71,72,89,90,91,92,59,60,61,62,79,80,81,82
     [LIST]: 1155.KL,1295.KL,7191.KL,0097.KL,2267.KL

I use httpclient to mimic the above request. 我使用httpclient模仿以上请求。

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    try {
        // TODO code application logic here
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, "Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_06");

        PostMethod post = new PostMethod("http://www.xxx.com:20000/%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage?");

        NameValuePair[] data = {
            new NameValuePair("SORT", "0,1,0,10,5,0,KL,0"),
            new NameValuePair("FIELD", "33,38,51,58,68,88,78,98,99,101,56,57,69,70,71,72,89,90,91,92,59,60,61,62,79,80,81,82"),
            new NameValuePair("LIST", "1155.KL,1295.KL,7191.KL,0097.KL,2267.KL")
        };
        post.setRequestBody(data);
        httpClient.executeMethod(post);
        System.out.println(post.getResponseBodyAsString());       
    }
    catch (Exception exp) {
        exp.printStackTrace();
    }
}

However, there are no respond from server side. 但是,服务器端没有响应。 I try to capture my own network request snapshot : 我尝试捕获自己的网络请求快照:

- Http: Request, POST /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage 
    Command: POST
  - URI: /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage
     Location: /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage 
    ProtocolVersion: HTTP/1.1
    UserAgent:  Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_06
    Host:  www.xxx.com:20000
    ContentLength:  234
  - ContentType:  application/x-www-form-urlencoded
     MediaType:  application/x-www-form-urlencoded
    HeaderEnd: CRLF
  - payload: HttpContentType =  application/x-www-form-urlencoded
     SORT: 0%2C1%2C0%2C10%2C5%2C0%2CKL%2C0
     FIELD: 33%2C38%2C51%2C58%2C68%2C88%2C78%2C98%2C99%2C101%2C56%2C57%2C69%2C70%2C71%2C72%2C89%2C90%2C91%2C92%2C59%2C60%2C61%2C62%2C79%2C80%2C81%2C82
     LIST: 1155.KL%2C1295.KL%2C7191.KL%2C0097.KL%2C2267.KL

It seems that there are two major different : 似乎有两个主要区别:

(1) URI (1)URI

(2) payload (2)有效载荷

Is there anything I miss out in my httpclient code? 我的httpclient代码中有什么我想念的吗?

After several experiment, I realize is that, httpclient perform URL encoding on my payload. 经过几次试验,我意识到,httpclient在我的有效负载上执行URL编码。 It explicitly turns 它明确地变成

1155.KL,1295.KL,7191.KL,0097.KL,2267.KL 1155.KL,1295.KL,7191.KL,0097.KL,2267.KL

to

1155.KL%2C1295.KL%2C7191.KL%2C0097.KL%2C2267.KL 1155.KL%2C1295.KL%2C7191.KL%2C0097.KL%2C2267.KL

To avoid this, I write a test code. 为了避免这种情况,我编写了一个测试代码。 The following test code works! 以下测试代码有效!

try {
    Socket socket = new Socket("www.xxx.com", 20000);
    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
    final String body = "[SORT]=0,1,0,10,5,0,KL,0&[FIELD]=33,38,51,58,68,88,78,98,99,101,56,57,69,70,71,72,89,90,91,92,59,60,61,62,79,80,81,82&[LIST]=1155.KL,1295.KL,7191.KL,0097.KL,2267.KL";
    final int length = body.length();
    final String s = "POST /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage? HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded\r\nCache-Control: no-cache\r\nPragma: no-cache\r\nUser-Agent: Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_06\r\nHost: www.xxx.com:20000\r\nAccept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\nConnection: keep-alive\r\nContent-Length: "+length+"\r\n\r\n" + body;
    out.println(s);

     BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
     while(true) {
          String ss = in.readLine();
          if (ss == null) break;
            System.out.println(ss);
     }

}
catch (Exception exp) {
}

The following is the captured snap-shot : 以下是捕获的快照:

  Frame: Number = 16, Captured Frame Length = 597, MediaType = ETHERNET 
+ Ethernet: Etype = Internet IP (IPv4),DestinationAddress:[7C-04-20-00-01-00],SourceAddress:[01-00-01-00-00-00]
+ Ipv4: Src = 10.176.181.55, Dest = 202.75.55.23, Next Protocol = TCP, Packet ID = 956, Total IP Length = 583
+ Tcp: Flags=...AP..., SrcPort=49253, DstPort=20000, PayloadLen=543, Seq=3920474750 - 3920475293, Ack=3537289604, Win=4380 (scale factor 0x2) = 17520
- Http: Request, POST /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage
    Command: POST
  + URI: /%5bvUpJYKw4QvGRMBmhATUxRwv4JrU9aDnwNEuangVyy6OuHxi2YiY=%5dImage?
    ProtocolVersion: HTTP/1.1
  + ContentType:  application/x-www-form-urlencoded
    Cache-Control:  no-cache
    Pragma:  no-cache
    UserAgent:  Mozilla/4.0 (Windows XP 5.1) Java/1.6.0_06
    Host:  n2ntbfd03.asiaebroker.com:20000
    Accept:  text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Connection:  keep-alive
    ContentLength:  164
    HeaderEnd: CRLF
  - payload: HttpContentType =  application/x-www-form-urlencoded
     [SORT]: 0,1,0,10,5,0,KL,0
     [FIELD]: 33,38,51,58,68,88,78,98,99,101,56,57,69,70,71,72,89,90,91,92,59,60,61,62,79,80,81,82
     [LIST]: 1155.KL,1295.KL,7191.KL,0097.KL,2267.KL

You are also missing the headers: Accept Cache-Control Pragma 您还缺少标题:Accept Cache-Control Pragma

The Accept header alone might be enough for the server to respond with something meaningful. 单独的Accept标头可能足以使服务器做出有意义的响应。 Try something like this for httpclient 4: 为httpclient 4尝试这样的操作:

   httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
      public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
        request.setHeader("Pragma","no-cache");
        request.setHeader("Cache-Control","no-cache");
        request.setHeader("Accept","text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
      }
    });

Or something like this for httpclient 3.x: 或对于httpclient 3.x来说是这样的:

method.setRequestHeader("Pragma","no-cache");
method.setRequestHeader("Cache-Control","no-cache");
method.setRequestHeader("Accept","text/html, image/gif, image/jpeg, *; q=.2, */*; 

Judging by the way the parameter names are displayed in your capture SORT vs [SORT]. 通过参数名称在捕获SORT与[SORT]中的显示方式来判断。 I suspect the server is expecting you to send the array with multiple form controls, like 我怀疑服务器期望您发送带有多个表单控件的数组,例如

SORT=0&SORT=1&SORT=0 ... SORT = 0&SORT = 1&SORT = 0 ...

They probably do this as a security measure because this can't be done with HTML. 他们可能将此作为一种安全措施,因为HTML无法做到这一点。

EDIT: You need to pass your parameters like this, 编辑:您需要像这样传递您的参数,

    NameValuePair[] data = {
        new NameValuePair("SORT", "0"),
        new NameValuePair("SORT", "1"),
        ...
        new NameValuePair("FIELD", "33"),
        ...
    };

How did you create the snapshot? 您是如何创建快照的? What do the brackets around the parameter names mean? 参数名称周围的括号是什么意思? Are there multiple values for the same parameter instead of one with comma separated value or should the parameter name be enclosed in brackets? 同一参数是否有多个值,而不是用逗号分隔的值?还是应该将参数名称括在方括号中?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM