java類比頁面登入

來源:互聯網
上載者:User

 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 />}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.