這個範例可以利用java.net.HttpURLconnection來摸擬瀏覽網頁
做form submit動作
public boolean doPost(String sURL,String data,String cookie,String referer,String charset)
post部份需要傳入
sURL:Action的url
data :要傳送的的資料也就是像id=123&test=456之類的
cookie:是否要傳送cookie資料,可為null,像 __utma=114386561.1334910113.1250671126.1251247266.1251279995.24;
referer:傳那裡來的,是一個網址,可為null
charset:傳送及取回的資料編碼為何
public boolean doGet(String sURL,String cookie,String referer,String charset)
跟post唯一不同的地方為
sURL:Action的url 再加上?data ,像http://www.aaa.com/123.jsp?id=123&test=456
ps.範例使用log4j,如果不會用的人可以直接把log4j部份改成System.out.println
private org.apache.log4j.Logger logger; public WebModule() { logger = org.apache.log4j.Logger.getLogger(this.getClass()); } public boolean doPost(String sURL, String data, String cookie, String referer, String charset) { boolean doSuccess = false; java.io.BufferedWriter wr = null; try { URL url = new URL(sURL); HttpURLConnection URLConn = (HttpURLConnection) url .openConnection(); URLConn.setDoOutput(true); URLConn.setDoInput(true); ((HttpURLConnection) URLConn).setRequestMethod("POST"); URLConn.setUseCaches(false); URLConn.setAllowUserInteraction(true); HttpURLConnection.setFollowRedirects(true); URLConn.setInstanceFollowRedirects(true); URLConn .setRequestProperty( "User-agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-TW; rv:1.9.1.2) " + "Gecko/20090729 Firefox/3.5.2 GTB5 (.NET CLR 3.5.30729)"); URLConn .setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); URLConn.setRequestProperty("Accept-Language", "zh-tw,en-us;q=0.7,en;q=0.3"); URLConn.setRequestProperty("Accept-Charse", "Big5,utf-8;q=0.7,*;q=0.7"); if (cookie != null) URLConn.setRequestProperty("Cookie", cookie); if (referer != null) URLConn.setRequestProperty("Referer", referer); URLConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); URLConn.setRequestProperty("Content-Length", String.valueOf(data .getBytes().length)); java.io.DataOutputStream dos = new java.io.DataOutputStream(URLConn .getOutputStream()); dos.writeBytes(data); java.io.BufferedReader rd = new java.io.BufferedReader( new java.io.InputStreamReader(URLConn.getInputStream(), charset)); String line; while ((line = rd.readLine()) != null) { System.out.println(line); } rd.close(); } catch (java.io.IOException e) { doSuccess = false; logger.info(e); } finally { if (wr != null) { try { wr.close(); } catch (java.io.IOException ex) { logger.info(ex); } wr = null; } } return doSuccess; } public boolean doGet(String sURL, String cookie, String referer, String charset) { boolean doSuccess = false; BufferedReader in = null; try { URL url = new URL(sURL); HttpURLConnection URLConn = (HttpURLConnection) url .openConnection(); URLConn .setRequestProperty( "User-agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; zh-TW; rv:1.9.1.2) " + "Gecko/20090729 Firefox/3.5.2 GTB5 (.NET CLR 3.5.30729)"); URLConn .setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); URLConn.setRequestProperty("Accept-Language", "zh-tw,en-us;q=0.7,en;q=0.3"); URLConn.setRequestProperty("Accept-Charse", "Big5,utf-8;q=0.7,*;q=0.7"); if (cookie != null) URLConn.setRequestProperty("Cookie", cookie); if (referer != null) URLConn.setRequestProperty("Referer", referer); URLConn.setDoInput(true); URLConn.setDoOutput(true); URLConn.connect(); URLConn.getOutputStream().flush(); in = new BufferedReader(new InputStreamReader(URLConn .getInputStream(), charset)); String line; while ((line = in.readLine()) != null) { System.out.println(line); } } catch (IOException e) { doSuccess = false; log.out.println(e); e.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (java.io.IOException ex) { logger.info(ex); } in = null; } } return doSuccess; } |
這篇文章太棒啦,讓我不必用到「HttpClient」就能讓網頁執行post動作,真是獲益良多,謝謝作者的教學。
還有其他文章的教學都很讚和很有幫助,有看有推,雖然懶的一篇一篇回,只有留這篇留言 意思意思,但總之每篇文章都大推,謝謝作者大方地傾囊相授啦~
[回應]
yku Replay:
七月 13th, 2010 at 9:41 上午
謝謝你的讚美...
如果有什麼特別需求也可以跟我講...
我有時間會做的範例的^^ (能力之內)
[回應]