Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By Hrithik516

Thread: Need Urgent Help with HttpPut request parameters

  1. #1
    Hrithik516 is offline Member
    Join Date
    Sep 2012
    Posts
    2
    Rep Power
    0

    Default Need Urgent Help with HttpPut request parameters

    Hi,

    I am trying to send params to a Servlet which accepts only PUT requests. When I append the params in the URL the values of those are passed to the Servlet but if I use the BasicNameValuePair to send the params it does not work, I mean the params values in the servlet are null.

    Here's the code which works
    Java Code:
     HttpClient httpClient = null;
    			HttpResponse response = null;
    			HttpPut httpPut = null;
    			StatusLine statusLine = null;
    			try { 
    				httpPut = new HttpPut("http://localhost:8080/Test_app/details?customerId=122&productId=pp");
    				
    				HttpParams httpParams = new BasicHttpParams();
    				int timeOut = 0;
    				HttpConnectionParams.setConnectionTimeout(httpParams, timeOut);	
    				httpClient = new DefaultHttpClient(httpParams);
    				
    			        response = httpClient.execute(httpPut);

    But I want to set the query params like below, parameters are going to the servlet but the values are null in the servlet
    Java Code:
    HttpClient httpClient = null;
    			HttpResponse response = null;
    			HttpPut httpPut = null;
    			StatusLine statusLine = null;
    			try { 
    				httpPut = new HttpPut("http://localhost:8080/Test_app/details");
    				
    				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    				nameValuePairs.add(new BasicNameValuePair("customerId", URLEncoder.encode("123456","UTF-8")));
    				nameValuePairs.add(new BasicNameValuePair("productId", URLEncoder.encode("pp12","UTF-8")));
    				httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
    				HttpParams httpParams = new BasicHttpParams();
    				int timeOut = 0;
    				HttpConnectionParams.setConnectionTimeout(httpParams, timeOut);	
    				httpClient = new DefaultHttpClient(httpParams);
    				
    			        response = httpClient.execute(httpPut);

    Also how do I send an xml in request body for the same HttpPUT request.

    Thanks for the help!!
    akhikhl likes this.

  2. #2
    akhikhl is offline Member
    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Default Re: Need Urgent Help with HttpPut request parameters

    Can you post the code with which you extract parameters? Don't need the whole stuff, only parameter extraction on the server side.
    What version of Apache HttpClient you are using?
    First thing to fix: don't encode each parameter separately, encode them all at once with proper call to UrlEncodedFormEntity:

    nameValuePairs.add(new BasicNameValuePair("customerId", "123456"));
    nameValuePairs.add(new BasicNameValuePair("productId", "pp12"));
    httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
    Last edited by akhikhl; 01-13-2014 at 09:10 PM.

Similar Threads

  1. How to major request parameters in jboss 4.0 forums
    By niravjoshi82 in forum New To Java
    Replies: 1
    Last Post: 10-14-2012, 04:59 PM
  2. Replies: 10
    Last Post: 01-24-2012, 10:02 PM
  3. Extracing xml rpc Servlet Request parameters
    By ypktm in forum Java Servlet
    Replies: 1
    Last Post: 11-24-2010, 09:03 AM
  4. urgent request
    By mudit222 in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 04-23-2010, 09:12 AM
  5. How to set request parameters to an URLConnection
    By somesh A in forum Networking
    Replies: 0
    Last Post: 04-10-2009, 08:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •