15

I am sending a call to a SMS gateway using their REST API. Everything is fine when I send a simple word like 'Hello', but if I add a space then I got in trouble. This because the URI cannot contain spaces.

What is the proper way to do what I need to do?

HttpClient httpclient = new DefaultHttpClient();
try {
    HttpGet httpget = new HttpGet("http://www.example.com/SecureREST/SimpleSMSsend?PhoneNumber=123&Message=hello how are you?");
httpget.addHeader(new BasicHeader("Accept", "application/json"));

// Create a response handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httpget, responseHandler);
System.out.println("----------------------------------------");
System.out.println(responseBody);
System.out.println("----------------------------------------");
} finally {
httpclient.getConnectionManager().shutdown();
}

Resulting on the IllegalArgumentException:

Exception in thread "main" java.lang.IllegalArgumentException
at java.net.URI.create(Unknown Source)
at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:69)
at main.main(main.java:36)
Caused by: java.net.URISyntaxException: Illegal character in query at index 97: https://www.example.com/SecureREST/SimpleSMSsend?PhoneNumber=123&Message=Hello, how are you?
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
... 3 more

Edit: As suggested by alexey28 I am using the Encoder, here is what I do now:

String query = "?PhoneNumber=123&Message=Hello, how are you?";
String host = "https://www.example.com/SecureREST/SimpleSMSsend";
String encodedUrl = host + URLEncoder.encode(query,"utf-8");
HttpGet httpget = new HttpGet(encodedUrl);

But is results in

Exception in thread "main" org.apache.http.client.HttpResponseException: **Bad Request**
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:67)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:54)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:735)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:709)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:700)
at main.main(main.java:47)

What am I doing wrong here?

The request once encoded: executing request https://www.example.com/SecureREST/SimpleSMSsend%3FPhoneNumber%3D123%26Message%3DHello%2C+how+are+you%3F

dukable
  • 3,968
  • 11
  • 31
  • 42

1 Answers1

26

Before sending use URLEncoder to encode URL parameters values:

String restUrl = URLEncoder.encode("You url parameter value", "UTF-8");

It will replace all your symbols including spaces -> '+' with proper one for URL

alexey28
  • 5,170
  • 1
  • 20
  • 25
  • 1
    Doing so results in a java.lang.IllegalStateException: Target host must not be null, or set in parameters. What I did is this: String restUrl = URLEncoder.encode("https://www.example.com/SecureREST/SimpleSMSsend?PhoneNumber=123&Message=Hello, how are you?", "UTF-8"); HttpGet httpget = new HttpGet(restUrl); – dukable May 15 '12 at 14:11
  • I did update my initial post to reflect the issue I am having now. – dukable May 15 '12 at 14:31
  • 11
    @user1385655 Only encode the message. URLEncoder will encode any character that has special meaning in an URL such as ? and &, so encoding the entire URL will make it invalid. E.g. `HttpGet("http://www.example.com/SecureREST/SimpleSMSsend?PhoneNumber=123&Message=" + URLEncoder.encode("hello how are you?","UTF-8"))` – erikxiv May 15 '12 at 14:41