public static void main(String[] args) {<br />HttpClient client = new HttpClient();<br />NameValuePair[] nameValuePairs = {<br />new NameValuePair("username", "aaa"),<br />new NameValuePair("passwd", "123456")<br />};<br />PostMethod postMethod = new PostMethod("登入url");<br />postMethod.setRequestBody(nameValuePairs);<br />int stats = 0;<br />try {<br />stats = client.executeMethod(postMethod);<br />} catch (HttpException e) {<br />e.printStackTrace();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}<br />postMethod.releaseConnection();//這裡最好把之前的資源放掉<br />CookieSpec cookiespec = CookiePolicy.getDefaultSpec();<br />Cookie[] cookies = cookiespec.match("網域名稱", 80/*連接埠*/, "/" , false , client.getState().getCookies());<br />for (Cookie cookie : cookies) {<br />System.out.println(cookie.getName() + "##" + cookie.getValue());<br />}</p><p>HttpMethod method = null;<br />String encode = "utf-8";//頁面編碼,按訪問頁面改動<br />String referer = "http://網域名稱";//http://www.163.com<br />method = new GetMethod("url2");//後續操作<br />method.getParams().setParameter("http.useragent","Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)");<br />method.setRequestHeader("Referer", referer);</p><p>client.getParams().setContentCharset(encode);<br />client.getParams().setSoTimeout(300000);<br />client.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(10, true));</p><p>try {<br />stats = client.executeMethod(method);<br />} catch (HttpException e) {<br />e.printStackTrace();<br />} catch (IOException e) {<br />e.printStackTrace();<br />}<br />if (stats == HttpStatus.SC_OK) {<br />System.out.println("提交成功!");</p><p>}<br />}