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

微信网站案例无极网络平台

微信网站案例,无极网络平台,悬浮网站底部代码,wordpress 文章页模板HttpClientr入门 介绍 HttpClient是Apache Jakarta Common下的子项目#xff0c;可以用来提供高效的#xff0c;最新的、功能丰富的支持HTTP协议的客户端编程工具包#xff0c;并且它支持HTTP协议的版本和建议。 依赖导入 dependencygroupIdorg.apache.…HttpClientr入门 介绍 HttpClient是Apache Jakarta Common下的子项目可以用来提供高效的最新的、功能丰富的支持HTTP协议的客户端编程工具包并且它支持HTTP协议的版本和建议。 依赖导入 dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactIdversion4.5.13/version /dependency核心API HttpClientHttpClientsCloseableHttpClientHttpGetHttpPost 发送请求步骤 创建HttpClient对象创建Http请求对象调用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,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.yayakq.cn/news/2459/

相关文章:

  • 微信网站建设费用计入什么科目如何在手机上做微电影网站
  • 网站导出链接查询天津公司建站
  • 西安建站模板厂家网站建设业务前景
  • 方又圆网站建设百度关键词数据
  • 企业建设网站的主要目的有哪些服务公司的经营范围
  • 温州做网站哪里好建造自己的网站
  • 建网站的8个详细步骤昌乐建设局网站
  • 一个ip怎么做多个网站百度seo关键词点击软件
  • 如何构建个人网站安阳贴吧论坛
  • php精品网站建设连云港市建设工程安全监督站网站
  • 泰州营销型网站工程造价信息网官网信息价
  • 全国水利建设市场信用信息平台门户网站比较有设计感的网站
  • 寺庙网站开发建设方案安徽智能网站建设制作
  • 石家庄网站推广专业鑫菲互动网站建设公司
  • 网站名称写什么网易邮箱163登录入口
  • 成都网站排名app开发多少钱?
  • 网站导航插件wordpress第三方账号
  • 动画做a视频在线观看网站太原市手机微网站建设
  • 广告网站制作多少钱卓博人才网手机版
  • 网站开发技术有哪些网站推广具体内容
  • 不用买服务器可以做网站wordpress 不同侧边栏
  • 网站主视觉网站开发岗位群
  • 网上做任务挣钱的网站北大青鸟网站建设
  • 如何优化网站代码梁平网站建设
  • 如何做网站不容易被攻击企业招聘
  • 网站开发例子网络黄页推广软件下载
  • 自己做优惠劵网站赚钱吗wix做的网站能扒下来
  • 汕头建站服务张裕网站建设的目标
  • o2o网站建设好么深圳网站公司招聘
  • 做网站能用的字体如何搭建一个企业子账号网站