我想用java自动登录网页,比如校内网。在网上找到下面这段代码,它本来是用来查询手机号码的。我试了试,是可以的。但是用来登录,总是报错。比较菜,请不吝赐教!谢谢。package recordRobot;import java.io.IOException;import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.*;/** * 提交参数演示 * 该程序连接到一个用于查询手机号码所属地的页面 * 以便查询号码段1330227所在的省份以及城市 */public class submitFormToWeb2 { public static void main(String[] args) throws IOException { HttpClient client = new HttpClient(); client.getHostConfiguration().setHost("www.renren.com", 80, "http"); HttpMethod method = getPostMethod();//使用POST方式提交数据 client.executeMethod(method); //打印服务器返回的状态 System.out.println(method.getStatusLine()); //打印结果页面 String response = new String(method.getResponseBodyAsString().getBytes("ISO-8859-1"),"UTF-8"); //打印返回的信息 System.out.println(response); method.releaseConnection(); } /** * 使用GET方式提交数据 * @return */ private static HttpMethod getGetMethod(){ return new GetMethod("/simcard.php?simcard=1330227"); } /** * 使用POST方式提交数据 * @return */ private static HttpMethod getPostMethod(){ PostMethod post = new PostMethod("/PLogin.do"); NameValuePair email = new NameValuePair("email","xxx"); post.setRequestBody(new NameValuePair[] { email } );
NameValuePair password = new NameValuePair("password","www"); post.setRequestBody(new NameValuePair[] { password } ); return post; }}
NameValuePair password = new NameValuePair("password","www"); post.setRequestBody(new NameValuePair[] { password } ); return post; }}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货