2013-04-04 16 views
5

Xin chào Tôi đang cố gắng gửi biểu mẫu sau bằng cách sử dụng HTTPURLConnection làm bài tập.gửi biểu mẫu bằng cách sử dụng 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> 

Tôi đang sử dụng HTTPURLConnection để làm nộp Đây là mã của tôi và lớp thử nghiệm của tôi

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

Và lớp dịch vụ của tôi mà làm công việc

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

Ai có thể tư vấn cho tại sao điều này không hoạt động khi gửi biểu mẫu. Có phải vì tôi không nhấp vào nút gửi có giá trị GO hay không. Và nếu đó là trường hợp sau đó làm thế nào để tôi thực sự nhấp vào nó bởi vì tôi mong đợi để gửi một cặp giá trị tên trên nhưng nút gửi không có một tên chỉ có giá trị.

Khi tôi đăng biểu mẫu từ mã này, tôi mong đợi phản hồi có chứa dữ liệu giống như khi tôi gửi biểu mẫu thủ công là dữ liệu trên trang này http://www.socialsecurity.gov/cgi-bin/popularnames.cgi.

Tuy nhiên khi chạy lớp thử, dữ liệu trong phản hồi tôi nhận được giống với trang gốc http://www.socialsecurity.gov/OACT/babynames/#ht=1.

Tất cả giúp đánh giá cao

Cảm ơn

+0

Tôi sẽ không sử dụng 'DataOutputStream' để viết nội dung yêu cầu, thay vào đó hãy sử dụng' PrintStream'. Ngoài ra, bạn nên kiểm tra phản hồi nhận được từ máy chủ trước khi đọc luồng đầu vào ('getResponseCode'). – Perception

+0

Đầu ra của lớp dịch vụ của bạn là gì và bạn mong đợi điều gì? – Friso

Trả lời