title: Sending HTTP Request GET/POST to form with Java?
body: <p>So I have this piece of code, and I got it to work, and now it basically allows me to send http post and get requests to almost any external website I want UNLESS the elements don't contain a name attribute. Here's an example: </p>
  
  <p>This is the Java code:</p>
  
  <pre><code>    public static String sendPostRequest(String url) {
  
      StringBuffer sb = null;
  
      try {
  
          String data = URLEncoder.encode("user", "UTF-8") + "="
                  + URLEncoder.encode("myUserName", "UTF-8") + "&amp;"
                  + URLEncoder.encode("submit", "UTF-8") + "="
                  + URLEncoder.encode("Submit", "UTF-8");
  
  
          URL requestUrl = new URL(url);
          HttpURLConnection conn = (HttpURLConnection) requestUrl
                  .openConnection();
          conn.setDoOutput(true);
          conn.setRequestMethod("GET");
  
          OutputStreamWriter osw = new OutputStreamWriter(
                  conn.getOutputStream());
          osw.write(data);
          osw.flush();
  
          BufferedReader br = new BufferedReader(new InputStreamReader(
                  conn.getInputStream()));
  
          String in = "";
          sb = new StringBuffer();
  
          while ((in = br.readLine()) != null) {
              sb.append(in + "\n");
          }
  
          osw.close();
          br.close();
      } catch (UnsupportedEncodingException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      return sb.toString();
  }
  </code></pre>
  
  <p>This is the form I'm trying to send a request to (it's a form on the w3schools site, this being the site <a href="http://www.w3schools.com/html/html_forms.asp" rel="nofollow">http://www.w3schools.com/html/html_forms.asp</a>): </p>
  
  <pre><code>&lt;form name="input0" target="_blank" action="html_form_action.asp" method="get"&gt;
  
  Username: 
  
  &lt;input type="text" name="user" size="20" /&gt;
  
  &lt;input type="submit" value="Submit" /&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Now because the Submit button doesn't have a name attribute, I can't send a proper HTTP Get/Post request to it (I know it's a get method in this case). What do I replace the String data with (what proper keys/values) so that it actually sends a request to this form? </p>