HttpClient 接口调用

摘要:
字符串URL=“http://127.0.0.1:8080/api“;//然后根据表名称HttpPosthttppost=newHttpPost(url);Listparams=newArrayList();params.add(newBasicNameValuePair(”comName“,comName))获取公司信息;
     String url = "http://127.0.0.1:8080/api";
        //然后根据表名获取公司信息
        HttpPost httppost = new HttpPost(url);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("comName", comName));
        HttpResponse httpResponse = null;
        HttpEntity httpEntity = null;
        try {
       //设置超时时间 httpclient4.5之后改为构建requestConfig对象        RequestConfig requestConfig
= RequestConfig.custom()             .setConnectTimeout(5000).setConnectionRequestTimeout(1000)             .setSocketTimeout(5000).build();        httppost.setConfig(requestConfig);
       HttpClient httpclient = HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler()).build();    httppost.setEntity(
new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpResponse = httpclient.execute(httppost); httpEntity = httpResponse.getEntity(); } catch (UnsupportedEncodingException e1) { logger.error(e1.getMessage()); } catch (ClientProtocolException e1) { logger.error(e1.getMessage()); } catch (IOException e1) { logger.error(e1.getMessage()); } if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {// 请求正常 try { String result = EntityUtils.toString(httpEntity); JSONObject data = JSONObject.fromObject(httpEntity); if("200".equals(data.get("code"))){ System.out.println("调用成功"); }else{ logger.error(data.get("message")); } } catch (Exception e) { logger.error(e.getMessage()); } }else { try {           logger.error(EntityUtils.toString(httpEntity)); } catch (ParseException e) { logger.error(e.getMessage()); } catch (IOException e) { logger.error(e.getMessage()); } }

免责声明:文章转载自《HttpClient 接口调用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ios 3DTouch基本使用教程node 升级下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

【转】在 ASP.NET Core 中使用 IHttpClientFactory 发出 HTTP 请求

https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/http-requests?view=aspnetcore-5.0#consumption-patterns 在 ASP.NET Core 中使用 IHttpClientFactory 发出 HTTP 请求 2021/01/21...

HttpClient 专题

HttpClient is a HTTP/1.1 compliant HTTP agent implementation based on HttpCore.It also provides reusable components for client-side authentication, HTTP state management, and HTTP...

java调用ip138实现ip地址查询

HttpClient不是一个浏览器,而是一个客户端HTTP传输类库。HttpClient作用是传输和接收HTTP消息。在HttpClient模块中用到了两个重要的类:HttpGet和HttpPost。这两个类分别用来提交HTTPGET和HTTPPOST请求。 接下来我们将使用以上的理论作为基础,然后进行一次连接http://www.ip138.com/ip...

后台发送请求,HttpClient的post,get各种请求,带header的请求

HttpClient依赖jar包: <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</...

httpClient带一个或多个参数实现

 听课视频:https://www.bilibili.com/video/av68932809?p=5 思路分析:先创建httpClient对象,再通过URIBuilder对象根上网址,最后一步带上一个多个参数。 如何写一个HttpClient[1]——URI的处理   1核心代码分: //1:创建httpClient对象CloseableHttpClie...

Java开发小技巧(五):HttpClient工具类

前言 大多数Java应用程序都会通过HTTP协议来调用接口访问各种网络资源,JDK也提供了相应的HTTP工具包,但是使用起来不够方便灵活,所以我们可以利用Apache的HttpClient来封装一个具有访问HTTP协议基本功能的高效工具类,为后续开发使用提供方便。 文章要点: HttpClient使用流程 工具类封装 使用实例 HttpClient...