0

This code working fine

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&text=testtest");

If i use space between parameter value. It throws exception

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&textParam=test test");

space between test test throws error. How to resolve?

Pinky
  • 683
  • 1
  • 9
  • 20

4 Answers4

2

You must URL encode the parameter in your URL; use %20 instead of the space.

HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&textParam=test%20test");

Java has a class to do do URL encoding for you, URLEncoder:

String param = "test test";
String enc = URLEncoder.encode(param, "UTF-8");

String url = "http://...&textParam=" + enc;
Jesper
  • 202,709
  • 46
  • 318
  • 350
1

Just use a %20 to represent a space.

This is all part of the URL encoding: http://www.w3schools.com/tags/ref_urlencode.asp

So you would want:

HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&text=test%20test");
Obicere
  • 2,999
  • 3
  • 20
  • 31
1

Use

URLEncoder.encode("test test","UTF-8")

So change your code to

HttpGet request = new HttpGet("http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&textParam="+URLEncoder.encode("test test","UTF-8"));

Note Don't Encode Whole url

URLEncoder.encode("http://...test"); // its Wrong because it will also encode the // in http://
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
0

Use %20 to indicate space in the URL, as space is not an allowed character. See the Wikipedia entry for character data in a URL.

HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(
    "http://localhost:8090/Servlet/ServletFirst?to=1234&from=567&textParam=test%20test");
Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64