当前位置:首页 > Web开发 > 正文

而不是HttpClient上 OkHttp超时设置: 直接在OkHttp上进行设置 privateOkHttpClien

2024-03-31 Web开发

为什么会写这篇文章,起因于和伴侣的聊天

技术图片

这又触及到我的常识盲区了,首先来一波面向百度学习,直接按照关键字httpclient和okhttp的区别、性能对照进行搜索,没有找到想要的答案,于是就去overstackflow上看看是不是有人问过这个问题,公然不会让你掉望的

技术图片

所以从使用、性能、超时配置方面进行对照

使用

HttpClient和OkHttp一般用于挪用其它处事,一般处事袒露出来的接口都为http,http常用请求类型就为GET、PUT、POST和DELETE,因此主要介绍这些请求类型的挪用

HttpClient使用介绍

使用HttpClient发送请求主要分为以下几法式:

创建 CloseableHttpClient东西或CloseableHttpAsyncClient东西,前者同步,后者为异步

创建Http请求东西

挪用execute要领执行请求,如果是异步请求在执行之前需挪用start要领

创建连接:

CloseableHttpClienthttpClient=HttpClientBuilder.create().build();

该连接为同步连接

GET请求:

@TestpublicvoidtestGet()throwsIOException{Stringapi="/api/files/1";Stringurl=String.format("%s%s",BASE_URL,api);HttpGethttpGet=newHttpGet(url);CloseableHttpResponseresponse=httpClient.execute(httpGet);System.out.println(EntityUtils.toString(response.getEntity()));}

使用HttpGet暗示该连接为GET请求,HttpClient挪用execute要领发送GET请求

PUT请求:

@TestpublicvoidtestPut()throwsIOException{Stringapi="/api/user";Stringurl=String.format("%s%s",BASE_URL,api);HttpPuthttpPut=newHttpPut(url);UserVOuserVO=UserVO.builder().name("h2t").id(16L).build();httpPut.setHeader("Content-Type","application/json;charset=utf8");httpPut.setEntity(newStringEntity(JSONObject.toJSONString(userVO),"UTF-8"));CloseableHttpResponseresponse=httpClient.execute(httpPut);System.out.println(EntityUtils.toString(response.getEntity()));}

POST请求:

添加东西

@TestpublicvoidtestPost()throwsIOException{Stringapi="/api/user";Stringurl=String.format("%s%s",BASE_URL,api);HttpPosthttpPost=newHttpPost(url);UserVOuserVO=UserVO.builder().name("h2t2").build();httpPost.setHeader("Content-Type","application/json;charset=utf8");httpPost.setEntity(newStringEntity(JSONObject.toJSONString(userVO),"UTF-8"));CloseableHttpResponseresponse=httpClient.execute(httpPost);System.out.println(EntityUtils.toString(response.getEntity()));}

该请求是一个创建东西的请求,需要传入一个json字符串

上传文件

@TestpublicvoidtestUpload1()throwsIOException{Stringapi="/api/files/1";Stringurl=String.format("%s%s",BASE_URL,api);HttpPosthttpPost=newHttpPost(url);Filefile=newFile("C:/Users/hetiantian/Desktop/学习/docker_practice.pdf");FileBodyfileBody=newFileBody(file);MultipartEntityBuilderbuilder=MultipartEntityBuilder.create();builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);builder.addPart("file",fileBody);//addPart上传文件HttpEntityentity=builder.build();httpPost.setEntity(entity);CloseableHttpResponseresponse=httpClient.execute(httpPost);System.out.println(EntityUtils.toString(response.getEntity()));}

通过addPart上传文件

DELETE请求:

@TestpublicvoidtestDelete()throwsIOException{Stringapi="/api/user/12";Stringurl=String.format("%s%s",BASE_URL,api);HttpDeletehttpDelete=newHttpDelete(url);CloseableHttpResponseresponse=httpClient.execute(httpDelete);System.out.println(EntityUtils.toString(response.getEntity()));}

请求的打消:

@TestpublicvoidtestCancel()throwsIOException{Stringapi="/api/files/1";Stringurl=String.format("%s%s",BASE_URL,api);HttpGethttpGet=newHttpGet(url);httpGet.setConfig(requestConfig);//设置超不时间//测试连接的打消longbegin=System.currentTimeMillis();CloseableHttpResponseresponse=httpClient.execute(httpGet);while(true){if(System.currentTimeMillis()-begin>1000){httpGet.abort();System.out.println("taskcanceled");break;}}System.out.println(EntityUtils.toString(response.getEntity()));}

挪用abort要领打消请求 执行功效:

taskcanceledcost8098mscDisconnectedfromthetargetVM,address:‘127.0.0.1:60549‘,transport:‘socket‘java.net.SocketException:socketclosed...【省略】 OkHttp使用

使用OkHttp发送请求主要分为以下几法式:

创建OkHttpClient东西

创建Request东西

将Request 东西封装为Call

通过Call 来执行同步或异步请求,挪用execute要领同步执行,挪用enqueue要领异步执行

创建连接:

privateOkHttpClientclient=newOkHttpClient();

GET请求:

@TestpublicvoidtestGet()throwsIOException{Stringapi="/api/files/1";Stringurl=String.format("%s%s",BASE_URL,api);Requestrequest=newRequest.Builder().url(url).get().build();finalCallcall=client.newCall(request);Responseresponse=call.execute();System.out.println(response.body().string());}

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30506.html