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

企业网站下载php网站后台制作

企业网站下载,php网站后台制作,网上推广怎么收费,店铺设计思路及创意HttpClient作用#xff1a; 发送HTTP请求 接收响应数据 HttpClient应用场景#xff1a; 使用扫描支付、查看地图、获取验证码、查看天气等功能,需要发起请求的时候 实现HttpClient HttpClient的maven坐标#xff1a; dependencygroupIdorg.apache.httpco…HttpClient作用 发送HTTP请求 接收响应数据 HttpClient应用场景 使用扫描支付、查看地图、获取验证码、查看天气等功能,需要发起请求的时候 实现HttpClient HttpClient的maven坐标 dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactIdversion4.5.13/version /dependency HttpClient的核心API HttpClientHttp客户端对象类型使用该类型对象可发起Http请求。 HttpClients可认为是构建器可创建HttpClient对象。 CloseableHttpClient实现类实现了HttpClient接口。 HttpGetGet方式请求类型。 HttpPostPost方式请求类型。 请求样例Get package com.sky.test;import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; 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 public class HttpClientTest {/*** 测试通过httpclient发送GET方式的请求*/Testpublic void testGET() throws Exception{//创建httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();//创建请求对象HttpGet httpGet new HttpGet(http://localhost:8080/user/shop/status);//发送请求接受响应结果CloseableHttpResponse response httpClient.execute(httpGet);//获取服务端返回的状态码int statusCode response.getStatusLine().getStatusCode();System.out.println(服务端返回的状态码为 statusCode);HttpEntity entity response.getEntity();String body EntityUtils.toString(entity);System.out.println(服务端返回的数据为 body);//关闭资源response.close();httpClient.close();} } POST /*** 测试通过httpclient发送POST方式的请求*/Testpublic void testPOST() throws Exception{// 创建httpclient对象CloseableHttpClient httpClient HttpClients.createDefault();//创建请求对象HttpPost httpPost new HttpPost(http://localhost:8080/admin/employee/login);JSONObject jsonObject new JSONObject();jsonObject.put(username,admin);jsonObject.put(password,123456);StringEntity entity new StringEntity(jsonObject.toString());//指定请求编码方式entity.setContentEncoding(utf-8);//数据格式entity.setContentType(application/json);httpPost.setEntity(entity);//发送请求CloseableHttpResponse response httpClient.execute(httpPost);//解析返回结果int statusCode response.getStatusLine().getStatusCode();System.out.println(响应码为 statusCode);HttpEntity entity1 response.getEntity();//使用EntityUtils对请求结果进行解析String body EntityUtils.toString(entity1);System.out.println(响应数据为 body);//关闭资源response.close();httpClient.close();} 封装的实体类  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/481683/

相关文章:

  • 网站推广的方式和管理方法公司注册核名
  • 帮人做钓鱼网站以及维护东莞住建局官网网站
  • 如何在百度里做推广网站个人怎样注册一家公司
  • 上海移动端网站建设西安专业做网站公司
  • 水果网站建设的策划书wordpress会员邮件通知
  • 合肥在线网站百色建设局网站
  • 梅河口市建设局网站华为网站搭建
  • gta5卖公司显示网站正在建设中公司网站制作有哪些
  • 免费推广网站大全下载c 视频播放网站开发
  • 做外贸网站维护费是多少做瞹瞹嗳视频网站
  • 国外好的网站空间台州论坛
  • 网络网站推广优化wordpress 文章 样式
  • 建设电影网站视频做暧视频网站大全
  • 深圳市建设银行网站国内产品网站
  • 东莞网站建设管理sem网络推广公司
  • seo建站平台哪家好做qq空间网站
  • 网站制作性价比哪家好中国最大的家装网站
  • 网站域名被注册公司运营管理方案
  • 网站建设评语商城微发布官网
  • 仿同程网 连锁酒店 网站模板系统模板html
  • 推广网app下载seo课程
  • 有效方法的小企业网站建设域名怎么拿来做网站
  • 网站版面如何布局做一个企业的网站怎么做的
  • 天门市城市建设管理局网站大连专业网页设计
  • 百度网站收录提交入口全攻略网站支付体现功能怎么做
  • 网站更改文章标题主流跨境电商平台有哪些
  • vue做直播网站上海最繁华的五个区
  • 做网站和微信小程序电子商务网站建设与管理的背景
  • 做网站国内阿里云虚拟主机多少钱东莞常平中学高中部
  • 用.net做购物网站灵山建设局网站