java模拟发送request

本文介绍了一个简单的Java程序,用于模拟发送HTTP GET请求到指定URL,并将响应内容保存为本地文件。该程序使用了Apache HttpClient库来执行网络请求,并通过异常处理确保了程序的健壮性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

可以模拟发送数据,我首先想到的是 外挂 作弊 呵呵

  1. import java.io.BufferedOutputStream;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import org.apache.commons.httpclient.*;
  7. import org.apache.commons.httpclient.methods.GetMethod;
  8. import org.apache.commons.httpclient.params.HttpMethodParams;
  9. public class GetSample {
  10.     public static void main(String[] args) {
  11.         // 构造HttpClient的实例
  12.         HttpClient httpClient = new HttpClient();
  13.         // 创建GET方法的实例
  14.         GetMethod getMethod = new GetMethod("http://www.baidu.com");
  15.         // 使用系统提供的默认的恢复策略
  16.         getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
  17.                 new DefaultHttpMethodRetryHandler());
  18.         try {
  19.             // 执行getMethod
  20.             int statusCode = httpClient.executeMethod(getMethod);
  21.             if (statusCode != HttpStatus.SC_OK) {
  22.                 System.err.println("Method failed: "
  23.                         + getMethod.getStatusLine());
  24.             }
  25.             // 读取内容
  26.             byte[] responseBody = getMethod.getResponseBody();
  27.             // 处理内容
  28.             writeFile(responseBody);
  29.             System.out.println(new String(responseBody));
  30.         } catch (HttpException e) {
  31.             System.out.println("Please check your provided http address!");
  32.             e.printStackTrace();
  33.         } catch (IOException e) {
  34.             e.printStackTrace();
  35.         } finally {
  36.             getMethod.releaseConnection();
  37.         }
  38.     }
  39.     static void writeFile(byte[] bt) throws IOException {
  40.         BufferedOutputStream bos = new BufferedOutputStream(
  41.                 new FileOutputStream(File.separator + "page.htm"));
  42.         bos.write(bt, 0, bt.length);
  43.         bos.flush();
  44.     }
  45. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值