3

Hi in my android project i m calling a webservice and sending get parameter through query string parameter , now problem is that if query string parameter value contains any white space then i am getting 505 error

                    URL url = new URL(urlstring.trim());                        
                    urlConnection = (HttpURLConnection) url.openConnection();
                    int response = urlConnection.getResponseCode();

I have one doubt if i use URLEncode(urlstring.trim(),"UTF-8")do i need to change my webservice code also ?

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
neeraj t
  • 4,654
  • 2
  • 27
  • 30

1 Answers1

8

You should encode only the values of your params:

    String urlString = "http://test.com?param1=" + URLEncoder.encode(value1, "UTF-8") + "&param2=" + URLEncoder.encode(value2, "UTF-8") ;

    URL url = new URL(urlstring.trim());                        
    urlConnection = (HttpURLConnection) url.openConnection();
    int response = urlConnection.getResponseCode();
sdabet
  • 18,360
  • 11
  • 89
  • 158
  • thank you fiddler for quick response, I was encoding complete url. – neeraj t Nov 07 '12 at 06:14
  • I made the same mistake with my rest service on Glassfish 5. I enconded the entire url and I got malformedUrlException, we must encode only the values of the params. – Broken_Window Feb 02 '21 at 00:50