当前位置: 首页 > news >正文

海南 网站制作北京住总第一开发建设有限公司网站首页

海南 网站制作,北京住总第一开发建设有限公司网站首页,如何用dw做网站前端,子域名做微信开放平台网站应用HttpClient 是Apache Jakarta Common 下的子项目#xff0c;可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包#xff0c;并且它支持 HTTP 协议最新的版本和建议。 就是用来发送http请求或者解析http响应的。 导入依赖#xff1a; dependency… HttpClient 是Apache Jakarta Common 下的子项目可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包并且它支持 HTTP 协议最新的版本和建议。 就是用来发送http请求或者解析http响应的。 导入依赖 dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactIdversion4.5.13/version /dependency 使用步骤 创建HttpClient实例 创建某种连接方法的实例 调用HttpClient实例的execute方法来执行请求方法 读取response 释放连接无论执行方法是否成功 发送get请求 创建HttpClient实例 CloseableHttpClient httpClient HttpClients.createDefault(); 创建GET请求方法实例 HttpGet httpGet new HttpGet(http地址); 调用HttpClient实例执行GET实例返回response CloseableHttpResponse response httpClient.execute(httpGet); 解析response // 获取状态码 int status response.getStatusLine().getStatusCode(); // 获取实例 HttpEntity entity response.getEntity(); // 获取实例数据 String html EntityUtils.toString(entity); 释放连接 response.close(); httpClient.close(); 发送post请求 与get请求方式基本相同只需在创建POST请求实例后使用实例对象调用setEntity(new StringEntity(参数));设置参数 创建HttpClient实例 CloseableHttpClient httpClient HttpClients.createDefault(); 创建POST请求方法实例 HttpGet httpPost new HttpPost(http地址); 设置参数 httpPost.setEntity(new StringEntity(参数)); 调用HttpClient实例执行GET实例返回response CloseableHttpResponse response httpClient.execute(httpPost); 解析response // 获取状态码 int status response.getStatusLine().getStatusCode(); // 获取实例 HttpEntity entity response.getEntity(); // 获取实例数据 String html EntityUtils.toString(entity); 释放连接 response.close(); httpClient.close(); 使用表单提交参数 提交表单一共就这几步 创建http实例 创建Post请求 设置表单参数并让Post携带 执行请求获取响应 解析响应 关闭资源 // 1. 创建HttpClient实例 CloseableHttpClient httpClient HttpClients.createDefault(); ​ // 2. 设置表单参数 ListNameValuePair listValues  new ArrayList(); listValues.add(new BasicNameValuePair(name, root)); listValues.add(new BasicNameValuePair(password, 123456)); ​ // 3. 创建HttpPost实例 HttpPost httpPost new HttpPost(http://httpbin.org/post); ​ // 4. 让Post携带表单参数 httpPost.setEntity(new UrlEncodedFormEntity(listValues, Consts.UTF_8)); ​ // 5. 获取HttpResponse响应 CloseableHttpResponse response httpClient.execute(httpPost); ​ // 6. 读response System.out.println(EntityUtils.toString(response.getEntity())); ​ // 7. 释放资源 response.close(); httpClient.close(); 封装好的HttpClient工具类 package com.sky.utils;import com.alibaba.fastjson.JSONObject; import org.apache.http.NameValuePair; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils;import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.Map;/*** Http工具类*/ public class HttpClientUtil {static final int TIMEOUT_MSEC 5 * 1000;/*** 发送GET方式请求* param url* param paramMap* return*/public static String doGet(String url,MapString,String paramMap){// 创建Httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();String result ;CloseableHttpResponse response null;try{URIBuilder builder new URIBuilder(url);if(paramMap ! null){for (String key : paramMap.keySet()) {builder.addParameter(key,paramMap.get(key));}}URI uri builder.build();//创建GET请求HttpGet httpGet new HttpGet(uri);//发送请求response httpClient.execute(httpGet);//判断响应状态if(response.getStatusLine().getStatusCode() 200){result EntityUtils.toString(response.getEntity(),UTF-8);}}catch (Exception e){e.printStackTrace();}finally {try {response.close();httpClient.close();} catch (IOException e) {e.printStackTrace();}}return result;}/*** 发送POST方式请求* param url* param paramMap* return* throws IOException*/public static String doPost(String url, MapString, String paramMap) throws IOException {// 创建Httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();CloseableHttpResponse response null;String resultString ;try {// 创建Http Post请求HttpPost httpPost new HttpPost(url);// 创建参数列表if (paramMap ! null) {ListNameValuePair paramList new ArrayList();for (Map.EntryString, String param : paramMap.entrySet()) {paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));}// 模拟表单UrlEncodedFormEntity entity new UrlEncodedFormEntity(paramList);httpPost.setEntity(entity);}httpPost.setConfig(builderRequestConfig());// 执行http请求response httpClient.execute(httpPost);resultString EntityUtils.toString(response.getEntity(), UTF-8);} catch (Exception e) {throw e;} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}/*** 发送POST方式请求* param url* param paramMap* return* throws IOException*/public static String doPost4Json(String url, MapString, String paramMap) throws IOException {// 创建Httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();CloseableHttpResponse response null;String resultString ;try {// 创建Http Post请求HttpPost httpPost new HttpPost(url);if (paramMap ! null) {//构造json格式数据JSONObject jsonObject new JSONObject();for (Map.EntryString, String param : paramMap.entrySet()) {jsonObject.put(param.getKey(),param.getValue());}StringEntity entity new StringEntity(jsonObject.toString(),utf-8);//设置请求编码entity.setContentEncoding(utf-8);//设置数据类型entity.setContentType(application/json);httpPost.setEntity(entity);}httpPost.setConfig(builderRequestConfig());// 执行http请求response httpClient.execute(httpPost);resultString EntityUtils.toString(response.getEntity(), UTF-8);} catch (Exception e) {throw e;} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}private static RequestConfig builderRequestConfig() {return RequestConfig.custom().setConnectTimeout(TIMEOUT_MSEC).setConnectionRequestTimeout(TIMEOUT_MSEC).setSocketTimeout(TIMEOUT_MSEC).build();}}
http://www.pierceye.com/news/341629/

相关文章:

  • 柯城区住房和城乡建设局网站wordpress仿fe素材
  • 黄岛建设局网站用什么建设网站
  • 桂林dj网站郑州上海做网站的公司
  • 进入江苏省住房和城乡建设厅网站网络舆情监测 toom
  • 延安市建设工程交易中心网站seo网络营销推广优化
  • 网站一条龙服务教育类网站前置审批
  • 安徽省建设厅网站首页wordpress和typecho
  • 网站开发考试题torrentkitty磁力猫引擎
  • 如何把电脑改成服务器 做网站微信网站背景图片
  • 淘宝客网站建设详细教程链接交换平台
  • 外贸门户网站深圳网站制作开发排名
  • 如何建设一个稳定的网站photoshop网页制作视频教程
  • 企业网站建设合作合同28招商加盟网
  • 国外设计灵感网站WordPress数据库添加管理员
  • 山东省建设备案网站审批表免费设计手写签名
  • 家庭电脑做网站深圳罗湖网站制作公司
  • 聊城做wap网站服务有哪些做特卖的网站有哪些
  • wordpress後台建站flash美食网站论文
  • 网站服务器ip地址在哪里看用记事本做电影介绍的网站
  • 重庆企业建站公司沧浪企业建设网站价格
  • 免费建单页网站厂房出租做推广什么网站好
  • jquery网站模板下载做的网站显示不了背景图片
  • 图书管理系统网站开发教程怎么创建自己的网址
  • 网站网站是怎么做的专业app开发制作团队
  • 平顺网站建设应届生在淮北招的网站建设类型岗位
  • 手机网站模板尺寸wordpress5.1更新
  • 微网站设计教育培训机构官网
  • 搭建论坛网站福州工程网站建设团队
  • 易语言跳到指定网站怎么做商业网站建设方案
  • 专业的饰品行业网站开发杭州市建设厅网站