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?

2

4 Answers 4

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;
Sign up to request clarification or add additional context in comments.

Comments

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");

Comments

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://

Comments

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.