title: HttpURLConnection GET request getting 400 Bad Request
body: <p>I am trying to do a GET request with some parameters in Java using HttpURLConnection. Everytime I do this however, I get a 400: Bad Request each time.
  What do I need to change to make it work?</p>
  
  <pre><code>String url = "http://www.awebsite.com/apath?p1=v1&amp;p2=v2&amp;p3=v3";
  HttpURLConnection conn = (HttpURLConnection)new URL(url).openConnection();
  conn.setDoInput(true);
  conn.setDoOutput(false);
  conn.setUseCaches(false);
  conn.setRequestMethod("GET");
  conn.setRequestProperty("Host", "www.awebsite.com");
  conn.setRequestProperty("User-Agent", "Mozilla/4.0");
  conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  conn.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
  conn.setRequestProperty("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
  conn.setRequestProperty("Keep-Alive", "115");
  conn.setRequestProperty("Connection", "keep-alive");
  BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  StringBuilder data = new StringBuilder();
  String s = "";
  while((s = br.readLine()) != null)
      data.append(s);
  String pageData = data.toString();
  </code></pre>
  
  <p>I have tried:</p>
  
  <ul>
  <li>Using URLEncoder on the whole query (after the ?) and just on the values.</li>
  <li>Setting the content length header.</li>
  <li>Setting the connection to use output and putting the query as the output.</li>
  </ul>