HttpClient模拟网页登录,并实现302重定向

直接上代码了,代码里面有详细的注释

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class test {

	public static void main(String[] args) {
		try {
			// 先访问首页,得到cookie
			// cookie信息自动保存在HttpClient中
			HttpClient httpClient = new HttpClient();
			PostMethod postMethod = new PostMethod("http://1.lexury2016.sinaapp.com/login");
			httpClient.executeMethod(postMethod);

			// 携带cookie访问登录网面
			postMethod = new PostMethod("http://1.lexury2016.sinaapp.com/login");
			// 设置登录的账号与密码
			NameValuePair[] nameValuePairs = { new NameValuePair("stucid", "2013150091"),
					new NameValuePair("stupassword", "ab1234") };
			postMethod.setRequestBody(nameValuePairs);
			// 设置请求编码为UTF-8
			postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
			// httpclient访问登录网页
			httpClient.executeMethod(postMethod);
			// 得到响应文本
			byte[] bytes = postMethod.getResponseBody();
			String html = new String(bytes);
			System.out.println(postMethod.getStatusCode());
			// 输出为302,也就是说网页发生了重定向
			
			// 得到重定向后的网页
			Header redirect = postMethod.getResponseHeader("location");
			String url = redirect.getValue();
			
			// 使用get请求,访问登陆后的页面
			GetMethod getMethod = new GetMethod(url);
			httpClient.executeMethod(getMethod);
			// 得到返回文本
			bytes = getMethod.getResponseBody();
			html = new String(bytes);
			System.out.println(html);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实现模拟登录网页,可以使用Java中的HttpURLConnection类或者Apache的HttpClient库来进行网络请求。 以下是使用HttpURLConnection类实现模拟登录的示例代码: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; public class LoginDemo { public static void main(String[] args) { try { // 设置登录请求的URL URL loginUrl = new URL("http://example.com/login"); HttpURLConnection conn = (HttpURLConnection) loginUrl.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); // 设置请求参数 String username = "your_username"; String password = "your_password"; String postData = "username=" + URLEncoder.encode(username, "UTF-8") + "&password=" + URLEncoder.encode(password, "UTF-8"); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(postData); wr.flush(); wr.close(); // 发送登录请求 int responseCode = conn.getResponseCode(); // 获取登录结果 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); // 处理登录结果 if (responseCode == 200) { // 登录成功 System.out.println("Login successful!"); } else { // 登录失败 System.out.println("Login failed!"); } } catch (Exception e) { e.printStackTrace(); } } } ``` 这段代码会向指定的登录URL发送POST请求,带上用户名和密码,然后获取服务器返回的结果并判断是否登录成功。 如果需要对返回的结果进行解析和处理,可以使用相关的HTML解析库,比如jsoup。 另外需要注意的是,有些网站可能会使用验证码等机制来防止自动登录,需要根据实际情况进行处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值