最近工作中使用到了两个jar包 httpclient.jar, httpcore.jar

HttpClient 的 abort(终止)程序示例

[java] view plaincopyprint?
/* * ==================================================================== * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements.  See the NOTICE file * distributed with this work for additional information * regarding copyright ownership.  The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License.  You may obtain a copy of the License at * *   http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied.  See the License for the * specific language governing permissions and limitations * under the License. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation.  For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */  package org.apache.http.examples.client;  import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;  /** * This example demonstrates how to abort an HTTP method before its normal completion. * 在HttpClient 普通完成前调用abort方法对其进行终止 */
public class ClientAbortMethod {  public final static void main(String[] args) throws Exception {  HttpClient httpclient = new DefaultHttpClient();  try {  HttpGet httpget = new HttpGet("http://www.apache.org/");  System.out.println("executing request " + httpget.getURI());  HttpResponse response = httpclient.execute(httpget);  HttpEntity entity = response.getEntity();  System.out.println("----------------------------------------");  System.out.println(response.getStatusLine());  if (entity != null) {  System.out.println("Response content length: " + entity.getContentLength());  }  System.out.println("----------------------------------------");  // Do not feel like reading the response body   // Call abort on the request object  // 不打算读取response body   // 调用request的abort方法  httpget.abort();  } finally {  // When HttpClient instance is no longer needed,  // shut down the connection manager to ensure  // immediate deallocation of all system resources  // 当HttpClient实例不再需要是,确保关闭connection manager,以释放其系统资源  httpclient.getConnectionManager().shutdown();  }  }  }

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;public class TTT
{/*** @param args* @throws IOException* @throws ClientProtocolException*/public static void main(String[] args) throws ClientProtocolException, IOException{// 创建HttpClient实例HttpClient httpclient = new DefaultHttpClient();// 创建Get方法实例HttpGet httpgets = new HttpGet("http://www.baidu.com");HttpResponse response = httpclient.execute(httpgets);HttpEntity entity = response.getEntity();if (entity != null){InputStream instreams = entity.getContent();String str = convertStreamToString(instreams);System.out.println(str);//终止http请求和响应httpgets.abort();}}public static String convertStreamToString(InputStream is){BufferedReader reader = new BufferedReader(new InputStreamReader(is));StringBuilder sb = new StringBuilder();String line = null;try{while ((line = reader.readLine()) != null){sb.append(line + "\n");}} catch (IOException e){e.printStackTrace();} finally{try{is.close();} catch (IOException e){e.printStackTrace();}}return sb.toString();}}

  

转载于:https://www.cnblogs.com/wuxinliulei/p/4859721.html

HttpClient模拟http请求相关推荐

  1. 一步步教你为网站开发Android客户端---HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新ListView...

    本文面向Android初级开发者,有一定的Java和Android知识即可. 文章覆盖知识点:HttpWatch抓包,HttpClient模拟POST请求,Jsoup解析HTML代码,动态更新List ...

  2. Java爬虫(二)-- httpClient模拟Http请求+jsoup页面解析

    博客 学院 下载 GitChat TinyMind 论坛 APP 问答 商城 VIP会员 活动 招聘 ITeye 写博客 发Chat 传资源 登录注册 原 Java爬虫(二)-- httpClient ...

  3. 使用HttpClient模拟POST请求

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...

  4. java模拟浏览器http请求_java使用HttpClient模拟浏览器请求

    代码如下,模仿10000次请求,为什么只成功了3次,求指教 按题主的代码,在第三次请求中,创建 HttpClientConnection 对象时就已经阻塞了,只成功3次实属正常(第三次是浏览器的请求) ...

  5. httpclient模拟post请求json封装表单数据

    好长时间不更博了,主要肚子里没什么好墨水,哈哈.废话不说上代码. 1 public static String httpPostWithJSON(String url) throws Exceptio ...

  6. 《Android开发从零开始》——31.模拟Http请求

    本节课的主要内容有: 1.讲解GET和Post请求方式 2.讲解通过HttpURLConnection模拟Http请求 3.讲解通过HttpClient模拟Http请求 课程下载地址:http://u ...

  7. java post json请求_java模拟post请求发送json

    java模拟post请求发送json,用两种方式实现,第一种是HttpURLConnection发送post请求,第二种是使用httpclient模拟post请求. 方法一: public stati ...

  8. 使用HttpClient模拟HTTP发送POST或GET请求

    在Java开发中,服务与服务之间进行调用,需要使用HttpClient发送HTTP请求,以下是使用Java实现模拟HTTP发送POST或GET请求的代码 1.pom.xml中导入HTTP依赖 < ...

  9. Python数据库访问公共组件及模拟Http请求

    前言 最近一段时间除了忙于工作之外,在业余时,迷上了python,对它的跨平台深深的吸引.经过一段时间的自我学习,了解了其基本的语法,便开始自己着手摆弄起来.主要想把以前对接的接口再实现一次,以便于在 ...

最新文章

  1. gateway java_基于SpringCloudGateway 实现的网关
  2. .net 互操作之p/invoke- 数据封送之字符串(2)
  3. centos7 无法启动网络(service network restart)错误解决办法
  4. 把内表 itab1 的 n1 到 n2 行内容附加到 itab2 内表中去.
  5. Slickflow.NET 开源工作流引擎基础介绍-.NET Core2.0 版本实现介绍
  6. [SpringSecurity]web权限方案_用户授权_注解使用
  7. python缺失值处理 fillna能否用scala来处理_数据清洗(一)丨处理缺失数据
  8. MTK 10A APP,菜单,资源添加
  9. 懒人——支付宝app支付
  10. Android 第一行代码(第2版).pdf 网盘
  11. 忍不住笑的笑话(last:20180419)
  12. 升级Win10后Markdown Pad2启动报错‘Awesomium.Windows.Controls.WebControl’解决方法
  13. 京东金融以应用为中心的DevOps体系建设(附PPT)
  14. springMVC中415错误
  15. c语言贪吃蛇答辩项目,贪吃蛇项目V1答辩.PDF
  16. 计算机基础知识竞赛报道,学校举办第十四届计算机基础知识竞赛
  17. Python爬取链家北京租房房价|保存为csv格式文件
  18. 写给三维建模入门小白的建议
  19. linux电脑滚轮不能用,图文详解电脑鼠标滚轮不动了怎么办_电脑鼠标滚轮不能用的三种解决方法-系统城...
  20. 【安装】win7 64位安装sql server2005图文教程

热门文章

  1. Linux 文件系统引起的云盘文件系统异常导致 MySQL 数据页损坏事故恢复复盘
  2. Hadoop- MapReduce分布式计算框架原理
  3. 【原创】DevExpress控件GridControl中的布局详解
  4. linux 必须掌握的60个命令
  5. SVD分解算法及其应用
  6. Gitlab部署和汉化以及简单运维
  7. C语言中的static 详细分析
  8. intelliJ idea如何安装、配置
  9. 解决SpringBoot使用Quartz无法注入Bean的问题
  10. WSS2.0升级到WSS3.0