how to get the value from HttpMethodParams

377 views Asked by At

At client side I use the following code:

HashMap<String, String> paramMap = new HashMap<>();
paramMap.put("userId", "1579533296");
paramMap.put("identity", "352225199101195515");
paramMap.put("phoneNum", "15959177178");
HttpClient client = new HttpClient();
PostMethod method = new PostMethod("http://localhost:8088/requestTest");
HttpMethodParams p = new HttpMethodParams();
for (Map.Entry<String, String> entry : paramMap.entrySet()) {
    p.setParameter(entry.getKey(), entry.getValue());
}
method.setParams(p);
client.executeMethod(method);

And the code of my server-side is like this:

@RequestMapping("/requestTest")
public void requestTest(HttpServletRequest request) throws IOException {
   String userId = request.getParameter("userId");
   String identity= request.getParameter("identity");
   String phoneNum= request.getParameter("phoneNum");
   System.out.println(userId+identity+phoneNum);
}

but I got the null value of userId,identity,and phoneNum,so how can I get the value of them? I know I can use method.setParameter(key,value) to set the parameter at client-side and use getParameter(key) to get the parameter value, but I just curious if there any way to get the value at server-side set by HttpMethodParams.

2

There are 2 answers

2
Sabir Khan On

I think , you are getting confused between user defined parameters set in HttpServletRequest and HttpMethodParams .

As per JavaDoc of - HttpMethodParams ,

This class represents a collection of HTTP protocol parameters applicable to HTTP methods.

These are predefined parameters specific to that HTTP method (see this)and has nothing to do with - HttpServletRequest parameters.

Request parameters need to be set as illustrated here

You have to also note that all these classes (HttpClient, PostMethod, HttpMethodParams etc ) that you are using on client side are from Apache to just be a convenient way to generate and call a HTTP end point but eventually what you will have on server side is a HttpServletRequest and there system is not Apache HttpClient specific.

So all you got on server side is to extract a named header or headers using - getHeaders() , getIntHeader() , getHeaderNames() , getDateHeader() , getProtocol() etc . Server side is standardized so you shouldn't see anything like - HttpMethodParams there.

0
SachinSarawgi On

You have to send your parameters using HttpServletRequest.

HttpMethodParams represent a collection of HTTP protocol parameters applicable to HTTP methods. List of Http method parameter can be found here.

But if you want to send it forcibly by HttpMethodParams you can set the JSON representation of your parameter in one of the variables of HttpMethodParameter and retrieve its value using that variable name.

Sample Code:

HttpMethodParams p = new HttpMethodParams();
p.setCredentialCharset("{userId":1579533296}"); 
//for loop not required
//your code

Now you can parse that JSON using ObjectMapper and get your required value.

Sample Code:

HttpMethodParams p = new HttpMethodParams();
JSONObject jsonObj = new JSONObject(p.getCredentialCharset()); 
jsonObj.get("userdId");

Note: This may work but not the recommended way.