java 模拟浏览器

现在需要用java模拟浏览器  去偷点数据来   有验证码登陆的

先简单的看了一下  有 HttpURLConnection 和HttpClient

再细看看HttpURLConnection 挺难弄的,决定从HttpClient入手

 

但是在eclipse的工程里面引入 httpclient包

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

问题待续--------------------------------

 

知道原因所在了 还少了logging的包 反正一共是三个包 少一个都不行 部署完毕 测试运行ok

 

看了一些别人的学习笔记    思路是

1 打开登陆的页面  获取附加码

2 将各个参数传入 登陆成功

3 打开需要查询的界面,余下就是时间参数的转换啦

 

 

/*

 * Created on 2010-12-2

 *

 * TODO To change the template for this generated file go to

 * Window - Preferences - Java - Code Style - Code Templates

 */

package com.Browser.bean;

 

import org.apache.commons.httpclient.*;

import org.apache.commons.httpclient.cookie.*;

import org.apache.commons.httpclient.methods.*;

 

/** 

 * 用来演示登录表单的示例 

 * @author Liudong 

 */

public class FormLoginDemo {

 

   public static void main(String[] args) throws Exception{

      HttpClient client = new HttpClient();

      client.getHostConfiguration().setHost( "XXXXX.XXXXXX.net" , 80,"http");

 

      /********打开登陆页面*获取附加码**********/

 

      HttpMethod method = new GetMethod("/");

      int status = client.executeMethod(method);

      String temp= method.getResponseBodyAsString();

      int FuJiaMaInt = temp.indexOf("<font color=#FFFFFF>");

      String FuJiaMa = temp.substring(FuJiaMaInt+26,FuJiaMaInt+30);

      System.out.println("FuJiaMa:"+FuJiaMa);

      method.releaseConnection();

      /*********输入参数*登陆*******/

      method = new GetMethod("/checkuser.jsp?acc_loginname=XXXX&acc_password=XXXX&checkCode="+FuJiaMa);

      status = client.executeMethod(method);

      System.out.println(method.getResponseBodyAsString());

 

      method.releaseConnection();

 

 

      // 访问所需的页面 main2.jsp 

      method =new GetMethod("/assess/main2.jsp ?zt=1291172382&date=2010-12-01");

      client.executeMethod(method);

      System.out.println(method.getResponseBodyAsString());

      method.releaseConnection();

   }

}

 

 

### Java 实现 POST 请求 为了使用 Java 模拟浏览器发送 POST 请求,`HttpURLConnection` 是一种常用的方式。下面是一个完整的例子来展示如何构建并发送带有参数的 HTTP POST 请求[^1]。 ```java import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; public class PostExample { private final String USER_AGENT = "Mozilla/5.0"; public static void main(String[] args) throws Exception { new PostExample().sendPost(); } // 发送POST请求到给定URL public void sendPost() throws Exception { String url = "http://example.com/api"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置基本属性 con.setRequestMethod("POST"); con.setRequestProperty("User-Agent", USER_AGENT); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // 设置为true表示允许输入和输出流操作 con.setDoOutput(true); // 构建表单数据字符串 String urlParameters = "param1=value1&param2=value2"; // 发送POST请求中的参数 try(OutputStream os = con.getOutputStream()) { byte[] input = urlParameters.getBytes("utf-8"); os.write(input, 0, input.length); } int responseCode = con.getResponseCode(); System.out.println("POST Response Code :: " + responseCode); if (responseCode == HttpURLConnection.HTTP_OK) { // 成功响应 // 处理成功后的逻辑... } else { // 错误处理... } } } ``` 此代码片段展示了创建 `HttpURLConnection` 对象的过程,并设置了必要的头部信息以及通过输出流写入要传递的数据。注意这里指定了 User-Agent 字符串模仿真实浏览器行为[^4]。此外,在设置连接对象时启用了输出模式 (`setDoOutput`) 来准备发送数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值