2013-04-04 72 views
5

您好我正在嘗試使用HTTPURLConnection作爲練習提交以下表單。使用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> 

我使用HttpURLConnection類做遞交這是我的代碼和我的測試類

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); 
} 
} 

我的服務類做的工作

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(); 
    } 
} 

任何人都可以提出建議,爲什麼這在提交表單時不起作用。是因爲我沒有點擊GO的提交按鈕。如果是這樣的話,我怎麼實際點擊它,因爲我希望發送一個名稱值對,但提交按鈕沒有名稱只有一個值。

當我從這段代碼發佈表單時,我期望響應中包含的數據與我手動進行表單提交時相同,這是本頁http://www.socialsecurity.gov/cgi-bin/popularnames.cgi上的數據。

但是,在運行測試類時,我得到的響應中的數據與原始頁面http://www.socialsecurity.gov/OACT/babynames/#ht=1相同。

所有幫助表示讚賞

感謝

+0

我不會使用'DataOutputStream'來寫請求體,而是使用'PrintStream'。另外,您應該在讀取輸入流('getResponseCode')之前檢查從服務器獲取的響應。 – Perception 2013-04-04 08:28:55

+0

您的服務班級的輸出是什麼以及您期望的是什麼? – Friso 2013-04-04 09:16:46

回答