java模拟发送request

来源:互联网 发布:上海淘宝托管 编辑:程序博客网 时间:2024/04/29 23:46

 

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

  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. }
原创粉丝点击