2013-04-04 18 views
5

こんにちは私は練習としてHTTPURLConnectionを使用して次のフォームを送信しようとしています。私はこれが私のコードと私のテストクラス、なぜこのHTTPURLConnectionを使用してフォームを送信

public class FormSubmitService { 

    public void doSubmit(String url, Map<String, String> data) throws IOException { 
     URL siteUrl = new URL(url); 
     HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection(); 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 
     conn.setUseCaches (true); 
     conn.setDoOutput(true); 
     conn.setDoInput(true); 

     DataOutputStream out = new DataOutputStream(conn.getOutputStream()); 

     Set keys = data.keySet(); 
     Iterator keyIter = keys.iterator(); 
     String content = ""; 
     for(int i=0; keyIter.hasNext(); i++) { 
      Object key = keyIter.next(); 
      if(i!=0) { 
       content += "&"; 
      } 
      content += key + "=" + URLEncoder.encode(data.get(key), "UTF-8"); 
     } 
     System.out.println(content); 
     out.writeBytes(content); 
     out.flush(); 
     out.close(); 
     BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     String line = ""; 
     while((line=in.readLine())!=null) { 
      System.out.println(line); 
     } 
     in.close(); 
    } 
} 

仕事は誰が助言することができない

public class FormSubmitServiceTest { 

@Test 
public void testSubmit() throws Exception { 
    String url = "http://www.socialsecurity.gov/OACT/babynames/#ht=1"; 
    Map<String, String> data = new HashMap<String, String>(); 
    data.put("year", "2010"); 
    data.put("top", "50"); 
    data.put("number", "n"); 

    FormSubmitService service = new FormSubmitService(); 
    service.doSubmit(url, data); 
} 
} 

そして、私のサービスクラスで提出を行うためのHttpURLConnectionを使用しています

<form name="popnames" method="post" action="/cgi-bin/popularnames.cgi" onsubmit="return  submitIt();"> 
<p> 
<label for="year">Birth Year:</label><br> 
<input type="text" name="year" size="5" maxlength="4" id="year" value="2011"> 
</p> 
<p> 
<label for="rank">Popularity:</label><br> 
<select name="top" size="1" id="rank"> 


<option value="20">Top 20</option> 
    <option value="50">Top 50</option> 
    <option value="100">Top 100</option> 
    <option value="500">Top 500</option> 
    <option value="1000">Top 1000</option> 
</select> 
</p> 
<fieldset> 
<legend>Name rankings may include:</legend> 
<input type="radio" name="number" value="p" id="percent"> 
<label for="percent">Percent of total births</label><br> 
<input type="radio" name="number" value="n" id="number"> 
<label for="number">Number of births</label> 
</fieldset> 
<hr> 
<input class="uef-btn uef-btn-primary" type="submit" value=" Go "> 
</form> 

フォームを提出する際には機能しません。 GOの値で送信ボタンをクリックしていないためですか?その場合、名前の値のペアを送信することを期待しているので、実際にはどのようにクリックするのですか?ただし、送信ボタンには名前だけの値はありません。

このコードからフォームを投稿すると、このページのデータであるフォーム提出を手動で行う場合と同じデータが返信されます(http://www.socialsecurity.gov/cgi-bin/popularnames.cgi)。

しかし、テストクラスを実行すると、取得する応答のデータは元のページhttp://www.socialsecurity.gov/OACT/babynames/#ht=1と同じになります。

すべてのヘルプは、私はあなたがあなたのテストケースにURLを変更した場合、それはすべてうまくいくhttp://www.socialsecurity.gov/cgi-bin/popularnames.cgiすることに気づくと思う

おかげ

+0

私は 'DataOutputStream'を使ってリクエスト本体を書いていますが、代わりに' PrintStream'を使います。また、入力ストリーム( 'getResponseCode')を読み取る前に、サーバーから受け取った応答をチェックする必要があります。 – Perception

+0

サービスクラスが提供している出力は何ですか?何を期待していますか? – Friso

答えて

2

を高く評価しました。

+0

私は今夜家に帰るときにこれをテストします。御時間ありがとうございます – user1107753