htmlunit学习

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.Page;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.WebRequest;
import com.gargoylesoftware.htmlunit.html.HtmlButton;
import com.gargoylesoftware.htmlunit.html.HtmlButtonInput;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlLabel;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gargoylesoftware.htmlunit.util.Cookie;


public class htmlunitTestRefer
{


/**
* @param args
* @throws IOException 
* @throws MalformedURLException 
* @throws FailingHttpStatusCodeException 
*/
/*
* public static void main(String[] args) { URL url=null; try { url = new
* URL(
* "http://p.yiqifa.com/c?s=55109db0&w=596861&c=17162&i=38061&l=0&e=zldmm&t=http://www.yintai.com"
* ); } catch (MalformedURLException e1) { // TODO Auto-generated catch
* block e1.printStackTrace(); } // String referrer =
* "http://www.google.com/"; WebClient webClient=new WebClient();

* System.out.println(webClient.getBrowserVersion().isFirefox()); WebRequest
* request=new WebRequest(url); request.setCharset("UTF-8");
* System.out.println(request.getCharset()); //
* request.setAdditionalHeader("Referer", referrer); HtmlPage page=null; try
* { page = webClient.getPage(request); URL url1=page.getUrl();
* webClient.getRefreshHandler().handleRefresh(page, url1, 0); //
* System.out.println(page.getWebResponse().getContentAsString()); //
* System.out.println(page.getWebResponse().getResponseHeaders()); String
* backHtml=page.getWebResponse().getContentAsString(); Document
* backDocument = Jsoup.parse(backHtml);
* System.out.println(backDocument.toString());

* Set<Cookie> cookie= webClient.getCookieManager().getCookies();
* System.out.println(cookie); //
* System.out.println(page.getBody().asText()); //
* System.out.println(page.get); } catch (FailingHttpStatusCodeException e)
* { // TODO Auto-generated catch block e.printStackTrace(); } catch
* (IOException e) { // TODO Auto-generated catch block e.printStackTrace();
* } // String docReferrer =
* (String)page.executeJavaScript("document.referrer"
* ).getJavaScriptResult(); // System.out.println(docReferrer); }
*/


// Get the first page
/*public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException
{


final WebClient webClient = new WebClient();
final HtmlPage page1 = webClient
.getPage("http://172.16.17.185:8083/backLogin.do");


// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getFormByName("myform");


final HtmlTextInput button = form.getInputByName("userName");
final HtmlPasswordInput textField = form.getInputByName("password");
final HtmlButtonInput login =  (HtmlButtonInput) form.getByXPath(".//*[@id='loginbtn']").get(0);
// Change the value of the text field
textField.setValueAttribute("admin");
button.setValueAttribute("admin");


// Now submit the form by clicking the button and get back the second
// page.
final HtmlPage page2 = login.click();
System.out.println(page2.getWebResponse().getContentAsString());
}*/

public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException
{


final WebClient webClient = new WebClient();
final HtmlPage page1 = webClient
.getPage("http://www.126.com/");


// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getForms().get(0);


final HtmlLabel button =(HtmlLabel) form.getByXPath("//*[@id='idPlaceholder']").get(0);
final HtmlLabel textField = (HtmlLabel) form.getByXPath("//*[@id='pwdPlaceholder']").get(0);
final HtmlButton login =  (HtmlButton) form.getByXPath("//*[@id='loginBtn']").get(0);
// Change the value of the text field
textField.type("");
button.type("");


// Now submit the form by clicking the button and get back the second
// page.
final HtmlPage page2 = login.click();
System.out.println(page2.getWebResponse().getContentAsString());
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值