title: How to pass spaces in a REST call using HttpGet in java
body: <p>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.</p>
  
  <p>What is the proper way to do what I need to do?</p>
  
  <pre><code>HttpClient httpclient = new DefaultHttpClient();
  try {
      HttpGet httpget = new HttpGet("http://www.example.com/SecureREST/SimpleSMSsend?PhoneNumber=123&amp;Message=hello how are you?");
  httpget.addHeader(new BasicHeader("Accept", "application/json"));
  
  // Create a response handler
  ResponseHandler&lt;String&gt; responseHandler = new BasicResponseHandler();
  String responseBody = httpclient.execute(httpget, responseHandler);
  System.out.println("----------------------------------------");
  System.out.println(responseBody);
  System.out.println("----------------------------------------");
  } finally {
  httpclient.getConnectionManager().shutdown();
  }
  </code></pre>
  
  <p>Resulting on the IllegalArgumentException:</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException
  at java.net.URI.create(Unknown Source)
  at org.apache.http.client.methods.HttpGet.&lt;init&gt;(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&amp;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.&lt;init&gt;(Unknown Source)
  ... 3 more
  </code></pre>
  
  <p><strong>Edit: As suggested by alexey28 I am using the Encoder, here is what I do now:</strong></p>
  
  <pre><code>String query = "?PhoneNumber=123&amp;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);
  </code></pre>
  
  <p>But is results in </p>
  
  <pre><code>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)
  </code></pre>
  
  <p>What am I doing wrong here?</p>
  
  <p>The request once encoded: executing request <a href="https://www.example.com/SecureREST/SimpleSMSsend%3FPhoneNumber%3D123%26Message%3DHello%2C+how+are+you%3F" rel="noreferrer">https://www.example.com/SecureREST/SimpleSMSsend%3FPhoneNumber%3D123%26Message%3DHello%2C+how+are+you%3F</a></p>