2012-07-26 42 views
2

這裏是我的代碼:使用Apache了HTTPClient提交棘手的表單數據

HttpClient httpclient = new DefaultHttpClient(); 

     HttpPost httppost = new HttpPost("http://www.webcitation.org/comb.php"); 

     try { 
      // Add your data 
      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair(??, ??)); 
      nameValuePairs.add(new BasicNameValuePair("fromform", "2")); 
      nameValuePairs.add(new BasicNameValuePair("email", "[email protected]")); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

      // Execute HTTP Post Request 
      HttpResponse response = httpclient.execute(httppost); 

      BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 

      String line = ""; 
      while((line = rd.readLine()) != null) { 
       System.out.println(line); 
      } 

     } catch (Exception e) { 
      System.out.println(e); 
     } 

這裏就是我試圖模仿形式的HTML代碼:http://www.webcitation.org/69Qsz3Tdn

我想提交的網址http://video.google.com/?hl=en&tab=wv,http://maps.google.com/maps?hl=en&tab=wl等通過Java到PHP文件並讀取響應。由於href沒有name s,我很難過。此外複選框沒有value s,所以我無法檢查它們是否被檢查。

感謝您的任何幫助!

+0

你是什麼意思? hrefs根本不會被提交,它們就是這樣 - 鏈接。我假設在服務器端有一些將URL映射到複選框名稱的東西。 – 2012-07-26 00:38:24

回答

0

你不會得到預期的迴應。

來自comb.php的響應取決於html表單。

在您的html表單中沒有指定值。所以我看不到comb.php如何測試複選框。

<td><input type="checkbox" name="cachingurl0"/></td> 
<td><a href="http://video.google.com/?hl=en&tab=wv">http://video.google.com/?hl=en&tab=wv</a></td> 

與值:

<td><input type="checkbox" name="cachingurl0" value="checked" /></td> 

1)還需要一個Java應用程序具有類似形式的GUI(例如擺動形式應用)。在那裏你可以選中或取消選中所需的複選框。

2.)您必須將適當的變量傳遞給comb.php。例如:

if (myCheckB0.checked) { nameValuePairs.add(new BasicNameValuePair("cachingurl0", "checked"));} 
if (myCheckB5.checked) { nameValuePairs.add(new BasicNameValuePair("cachingurl5", "checked"));