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

网站开发目的与意义建设网站需要多少钱济南兴田德润地址

网站开发目的与意义,建设网站需要多少钱济南兴田德润地址,网站推广的公司哪家好,网站建设关键词分类HttpClientr入门 介绍 HttpClient是Apache Jakarta Common下的子项目&#xff0c;可以用来提供高效的&#xff0c;最新的、功能丰富的支持HTTP协议的客户端编程工具包&#xff0c;并且它支持HTTP协议的版本和建议。 依赖导入 <dependency><groupId>org.apache.…

HttpClientr入门

介绍

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

依赖导入
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version>
</dependency>
核心API
  • HttpClient
  • HttpClients
  • CloseableHttpClient
  • HttpGet
  • HttpPost
发送请求步骤:
  1. 创建HttpClient对象
  2. 创建Http请求对象
  3. 调用HttpClient的execute方法发送请求
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
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.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest
@Slf4j
public class HttpClientTest {@Testpublic void HttpGET() throws Exception {//1.创建请求HttpClient对象CloseableHttpClient httpClient = HttpClients.createDefault();//2.创建Http请求对象HttpGet httpGet = new HttpGet("http://localhost:8080/user/shop/status");//3.调用HttpClient的execute方法发送请求CloseableHttpResponse response = httpClient.execute(httpGet);//获取数据//获取响应的状态码int code = response.getStatusLine().getStatusCode();System.out.println("返回的状态码是:"+code);//获取响应的数据HttpEntity entity = response.getEntity();String body = EntityUtils.toString(entity);System.out.println("服务器返回的数据为:"+body);//关闭请求httpClient.close();response.close();}@Testpublic void HttpPost() throws Exception{//创建HttpClient对象CloseableHttpClient httpClient = HttpClients.createDefault();//创建Http请求对象HttpPost post = new HttpPost("http://localhost:8080/admin/employee/login");//构建请求的参数JSONObject jsonObject = new JSONObject();jsonObject.put("username","admin");jsonObject.put("password","123456");StringEntity stringEntity = new StringEntity(jsonObject.toString());//指定编码格式stringEntity.setContentEncoding("utf-8");//数据格式stringEntity.setContentType("application/json");post.setEntity(stringEntity);//发送请求CloseableHttpResponse response = httpClient.execute(post);//获取响应回来的参数int statusCode = response.getStatusLine().getStatusCode();System.out.println("状态码:"+statusCode);HttpEntity entity = response.getEntity();String body = EntityUtils.toString(entity);System.out.println("post请求响应回来的参数:"+body);//关闭服务httpClient.close();post.clone();}
}

HttpClientUtil 工具类,使用的时候直接调用

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,Map<String,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, Map<String, 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) {List<NameValuePair> paramList = new ArrayList();for (Map.Entry<String, 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, Map<String, 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.Entry<String, 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.yayakq.cn/news/45793/

相关文章:

  • 怎么做网站教程图片怎么查询网站是否被降权
  • 做视频网站赚做视频网站赚外贸做网站要多久做好
  • 深圳市盐田区建设局网站北京市市场监督管理局官网
  • 做一网站要什么软件vue.js做网站
  • 无成本搭建属于自己的网站用了wordpress的网站
  • 品牌网站建设报价单aspcms中引文 网站修改配置
  • 网站的联系我们怎么做做网站需要多少台服务器
  • 什么是网络推广seo标题优化关键词怎么选
  • 营销型网站建设需要注意什么百度快速收录办法
  • 网站开发技术背景介绍百度账号登陆入口
  • 计算机网站建设 是什么意思十大免费软件免费下载
  • 网站域名改了以后新域名301迁安建设局官方网站
  • 国外网站推广平台有哪些办公室装修设计方案
  • 网站建设维护管理软件国外好的网站空间
  • 网站设计方案范本波兰网站后缀
  • 企业网站建设cms站现在哪里大搞建设
  • 网站开发策划莒县网页定制
  • 晋城市住建设局网站韩国网站设计欣赏
  • 泰州网站设计培训网站主机要多少钱
  • 成全视频免费观看在线看2024年新年贺词保定seo公司
  • 华强北做电子网站网站开发知识版权
  • 建筑工程网站模板手机网站快速建站
  • 杭州网络营销网站建立网站需要多少钱 纠正错误湖南岚鸿
  • 佛山公益网站制作武功网站建设
  • 网站内容建设策略网站制作完成后
  • 咸宁公司网站建设做字画的网站
  • 企业网站开发报价单网站如何做关键词排名
  • 酒店网站素材阿里网站建设费用
  • 常宁城乡建设局网站查询安仁网络推广软件定制开发
  • 深圳市南山区做网站的小公司WordPress登录页提示