2012-11-06 106 views

回答

5

嘗試硒。 Selenium在自動化Web瀏覽器方面非常出色。

http://seleniumhq.org/

還與純粹的Java支持。但不僅如此。

當涉及到自定義的方法,請參閱...

String urlParameters = "param1=a&param2=b&param3=c"; 
String request = "http://example.com/index.php"; 
URL url = new URL(request); 
HttpURLConnection connection = (HttpURLConnection) url.openConnection();   
connection.setDoOutput(true); 
connection.setDoInput(true); 
connection.setInstanceFollowRedirects(false); 
connection.setRequestMethod("POST"); 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
connection.setRequestProperty("charset", "utf-8"); 
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); 
connection.setUseCaches (false); 

DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); 
wr.writeBytes(urlParameters); 
wr.flush(); 
wr.close(); 
connection.disconnect(); 

源(Java - sending HTTP parameters via POST method easily

+0

我們可以將數據傳遞給網頁,即使用post方法傳遞數據嗎? –

0

您可以使用Apache了HTTPClient - 看看這個例子:

http://hc.apache.org/httpclient-3.x/methods/post.html

這使您可以模擬將完全填充的表單直接提交到目標頁面並獲取結果。

記住,通話結束後,你必須抓住和會話cookie存儲在響應,並將其重新提交到要「訪問」留「登錄」

1

,如果你的網頁下面的頁面使用GET方法來接受數據(也就是來自URL),只需連接到網頁給數據要傳遞:

http://www.mysite.com/mypage.html?data0=data0,data1=data1

如果網頁使用POST事情變得有點複雜:您必須僞造一個適當的HTML請求,並在頭中包含所有數據(如POST方法所要求的)

-1

我想說明我會如何將輸入傳遞給HTML。我通常使用python將請求發送到需要輸入數據的頁面。在此之前,您需要知道是否需要提供網絡cookie,如果是,請複製cookie,如果您需要登錄,否則請檢查。完成後,您需要知道輸入區域的字段名稱,因爲您將使用它們來使用腳本進行POST或GET數據。以下是示例用法。

import urllib 
import urllib2 
import string 
headers = {'Cookie': 'You cookies if you need'} 
values = {'form_name':'sample text', 'submit':''} 
data = urllib.urlencode(values) 
req = urllib2.Request('website where you making request to',data,headers) 

opener1 = urllib2.build_opener() 


page1=opener1.open(req) 
#OPTIONAL 
htmlfile=page1.read() 

fout = open('MYHTMLFILE.html', "wb") 
fout.write(htmlfile) 
fout.close() 
相關問題