httpclient

How exactly are timeouts handled by HttpClient?

泄露秘密 提交于 2020-02-01 03:07:09
问题 So there are two timeout properties that can be set on HttpClient: HttpClient.TimeOut and WebRequestHandler.ReadWriteTimeout. The first one is just a timeout for the whole request/response, so if a download/upload takes longer than that, I am out of luck and get cut off in the middle of transfer, no questions asked. This can obviously be overridden by setting the timeout to Infinite, but I am not certain what consequences does that have. Now the latter (ReadWriteTimeOut) - at least to my

C# HttpClient PUT

两盒软妹~` 提交于 2020-02-01 00:13:10
问题 For some reason my below code that used to work now consequently raises an exception: public static async Task<string> HttpPut(string inUrl, string inFilePath) { using (var handler = new HttpClientHandler { AllowAutoRedirect = false }) { using (var client = new HttpClient(handler)) { //var content = new StreamContent(new FileStream(inFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, bufferSize: 4096, useAsync: true)); using (var content = new StreamContent(new FileStream(inFilePath,

org.apache.http.ProtocolException: Target host is not specified

时间秒杀一切 提交于 2020-01-30 04:06:32
问题 I have written a simple httprequest/response code and I am getting this below error. I have referenced httpclient, httpcore, common-codecs and common-logging in my classpath. I am very new to java and have no clue what is going on here. Please help me. Code: import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.HttpResponse; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.Header; import org.apache.http

org.apache.http.ProtocolException: Target host is not specified

馋奶兔 提交于 2020-01-30 04:06:20
问题 I have written a simple httprequest/response code and I am getting this below error. I have referenced httpclient, httpcore, common-codecs and common-logging in my classpath. I am very new to java and have no clue what is going on here. Please help me. Code: import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.HttpResponse; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.Header; import org.apache.http

C# HttpClient StreamContent用法

纵然是瞬间 提交于 2020-01-29 08:45:23
//var url = "http://demo.com/api/streamcontent"; //var item =new MyClass(); HttpClient httpClient = new HttpClient(); using (HttpRequestMessage request = new HttpRequestMessage()) { request.Method = HttpMethod.Post; request.RequestUri = new Uri(url); using (MemoryStream ms = new MemoryStream()) { //使用二进制序列化将你需要转送的对象转换成Stream BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(ms, item); ms.Position = 0; //重置ms的position,便于StreamContent读取流内容 request.Content = new StreamContent(ms, (int)ms.Length); var res = httpClient.SendAsync(request).Result; res

httpclient 连接池测试

流过昼夜 提交于 2020-01-29 00:29:51
为什么需要使用http连接池 1、降低延迟:如果不采用连接池,每次连接发起Http请求的时候都会重新建立TCP连接(经历3次握手),用完就会关闭连接(4次挥手),如果采用连接池则减少了这部分时间损耗,别小看这几次握手,本人经过测试发现,基本上3倍的时间延迟 2、支持更大的并发:如果不采用连接池,每次连接都会打开一个端口,在大并发的情况下系统的端口资源很快就会被用完,导致无法建立新的连接 连接池实例 连接池管理器代码 import org.apache.http.config.Registry; import org.apache.http.config.RegistryBuilder; import org.apache.http.conn.socket.ConnectionSocketFactory; import org.apache.http.conn.socket.LayeredConnectionSocketFactory; import org.apache.http.conn.socket.PlainConnectionSocketFactory; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.impl.client

Web接口测试-HttpClient

亡梦爱人 提交于 2020-01-28 13:44:12
要实现Web接口测试的自动化有许多方式,比如利用Jmeter、Loadrunner等测试工具都能够实现接口的自动化测试,我们也可以利用一些开源的框架来实现接口的自动化测试,比如我们现在要说的这个HttpClient,   HttpClient是一个功能丰富支持HTTP协议的客户端编程工具包,具备以下主要功能:   1)封装实现了所有HTTP的方法,如GET,POST,PUT,HEAD   2)支持redirect,会话保持   3)支持文件上传 它是Apache下面开发的,更多信息大家可以上官网瞅瞅。 既然谈到了接口测试,首先我们得明白何为接口测试,简单一句话啊就是 测试外部系统 与 内部系统 或 一个系统中不同的功能模块 之间的交互点,我们测试的重点是检查数据的交换、传递、控制管理的过程,以及系统间的相互的逻辑依赖关系。 利用HttpClient我们做的接口测试主要是服务器端与客户端交互的方式,即浏览器或其它客户端与Web服务器之间的交互协议,这里讲的主要是HTTP协议,Http协议常用的请求方法有 Post 和 Get, 一般情况下从客户端传向服务器端的用Post, 从服务器端传出的用Get方法,这些都是一般情况下,测试的过程中还得具体情况具体分析。 顺便插一嘴,我们看看采用Get和Post方法来提交表单时的区别,明白了Get和Post的区别以后

HttpClient详细解释

泪湿孤枕 提交于 2020-01-28 00:58:03
Http协议的重要性相信不用我多说了,HttpClient相比传统JDK自带的URLConnection,增加了易用性和灵活性(具体区别,日后我们再讨论),它不仅是客户端发送Http请求变得容易,而且也方便了开发人员测试接口(基于Http协议的),即提高了开发的效率,也方便提高代码的健壮性。因此熟练掌握HttpClient是很重要的必修内容,掌握HttpClient后,相信对于Http协议的了解会更加深入。 org.apache.commons.httpclient.HttpClient与org.apache.http.client.HttpClient的区别 Commons的HttpClient项目现在是生命的尽头,不再被开发, 已被Apache HttpComponents项目HttpClient和的HttpCore 模组取代,提供更好的性能和更大的灵活性。 一、简介 HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中,比如Apache Jakarta上很著名的另外两个开源项目Cactus和HTMLUnit都使用了HttpClient。 下载地址: http://hc.apache.org

Android网络连接之HttpURLConnection和HttpClient

只谈情不闲聊 提交于 2020-01-27 04:50:41
1.概念 HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。在 JDK 的 java.net 包中已经提供了访问 HTTP 协议的基本功能:HttpURLConnection。但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。 除此之外,在 Android中, androidSDK中集成了 Apache的 HttpClient模块,用来提供高效的、最新的、功能丰富的支持 HTTP 协议工具包,并且它支持 HTTP 协议最新的版本和建议。使用 HttpClient可以快速开发出功能强大的 Http程序。 2.区别 HttpClient是个很不错的开源框架,封装了访问http的请求头,参数,内容体,响应等等, HttpURLConnection是java的标准类,什么都没封装,用起来太原始,不方便,比如 重访问的自定义 ,以及一些高级功能等。 URLConnection HTTPClient Proxies and SOCKS Full support in Netscape browser, appletviewer, and applications (SOCKS: Version 4 only); no additional limitations from

HttpClient 教程 (五)

帅比萌擦擦* 提交于 2020-01-27 02:52:21
第五章 HTTP客户端服务 5.1 HttpClient门面 HttpClient接口代表了最重要的HTTP请求执行的契约。它没有在请求执行处理上强加限制或特殊细节,而在连接管理,状态管理,认证和处理重定向到具体实现上留下了细节。这应该使得很容易使用额外的功能,比如响应内容缓存来装饰接口。 DefaultHttpClient是HttpClient接口的默认实现。这个类扮演了很多特殊用户程序或策略接口实现负责处理特定HTTP协议方面,比如重定向到处理认证或做出关于连接持久化和保持活动的持续时间决定的门面。这使得用户可以选择使用定制,具体程序等来替换某些方面默认实现。 DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { long keepAlive = super.getKeepAliveDuration(response, context); if (keepAlive == -1) { //