package http;import com.gargoylesoftware.htmlunit.WebClient;import com.gargoylesoftware.htmlunit.html.HtmlElement;import com.gargoylesoftware.htmlunit.html.HtmlPage;public class http { public static void main(String[] args) throws Exception { WebClient webClient = new WebClient();//创建WebClient HtmlPage page = webClient.getPage("http://*****/wp-admin/"); //打开百度 //获得name为"登陆"的html元素 HtmlElement htmlElement = page.getElementByName("wp-submit"); page = htmlElement.click();//调用click()方法 //获得name为"username"的html元素 HtmlElement usernameEle = page.getElementByName("user_login"); //获得id为"password"的html元素 HtmlElement passwordEle = (HtmlElement) page.getElementById("user_pass"); usernameEle.focus(); //设置输入焦点 usernameEle.type("***"); //填写值 passwordEle.focus(); //设置输入焦点 passwordEle.type("****"); //填写值 //获得name为"登陆"的元素 HtmlElement submitEle = page.getElementByName("登陆"); //点击“登陆” page = submitEle.click(); String result = page.asXml();//获得click()后的html页面(包括标签) if(result.contains("登陆成功!")){ System.out.println("登陆成功"); }else{ System.out.println("登陆失败"); } }}
http://www.cnblogs.com/cha1r/p/3410966.html
package http;import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;public class httptests { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { try { HttpClient client = new DefaultHttpClient(); //构建一个Client HttpPost post = new HttpPost("http://mail.163.com/"); //构建一个POST请求 //构建表单参数 ListformParams = new ArrayList (); formParams.add(new BasicNameValuePair("username", "********")); formParams.add(new BasicNameValuePair("password", "*****")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formParams, "UTF-8");//将表单参数转化为“实体” post.setEntity(entity); //将“实体“设置到POST请求里 HttpResponse response = client.execute(post);//提交POST请求 HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体" String content = EntityUtils.toString(result);; //用httpcore.jar提供的工具类将"实体"转化为字符串打印到控制台 System.out.println(content); if(content.contains("登陆成功")){ System.out.println("登陆成功!!!"); } } catch (Exception e) { e.printStackTrace(); } // HttpClient httpclient = new HttpClient(); // PostMethod httpPost =new PostMethod("******/abc"); // NameValuePair[] param = { new NameValuePair("username", "vip")}; // httpPost.setRequestBody(param); // httpclient.executeMethod(httpPost); } }