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

php快速建站系统深圳市宝安区西乡街道邮政编码

php快速建站系统,深圳市宝安区西乡街道邮政编码,图片网站该如何做seo优化,网页设计的目的与要求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/105475/

相关文章:

  • 网站竞争对手的选定一般参考什么标准的网站的推广方案的内容有哪些
  • 把别人的图片拿来做网站wordpress 覆盖原始图片对比效果
  • 江门市建设工程备案网站乌兰浩特网站开发
  • 个人介绍微电影网站模板免费下载一个网站的建设成本
  • 搬瓦工怎么做网站百度小程序开发教程
  • 织梦网站被攻击小型门户网站建设方案
  • 长沙网站托管公司排名静态网站结构如何更新
  • 网站建设 宜宾湖州网站建设培训教程
  • 网站建设脚本什么叫营销型网站
  • 沧州网站建设专业的公司4000-262-seo优化与品牌官网定制
  • 网页游戏推广网站怎么做如何创建平台类网站
  • 十堰英文网站建设学校教育网站模板
  • 学校网页网站模板免费下载微信的微网站
  • 网站开发的解决方案小型行业网站建设维护成本
  • 浏阳市网站建设百度中心人工电话号码
  • 高端外贸网站制作智慧团建网站没有验证码
  • 做网站需要买服务器么网站建设营销外包公司排名
  • 网站建设中要多使用图片258网站建设
  • 做多语言网站教程开网页慢是什么原因
  • 汽车商城网站模板seo公司网站推广
  • 网站备案查询工信部官网网站建设费用推荐网络专业
  • 网站开发费用是研发费用国内做seo最好公司
  • 什么是网站建设公司企业网站后台管理
  • 做网站阿里巴巴好还是百度好柠檬网络科技网站建设
  • 做个网站要多少钱网站建设工作 方案
  • 一个主体如何添加网站室内设计公司的名字
  • 中国建设学会网站洛阳市住房和城乡建设局网站
  • 北京网站优化方式做物流的网站都有什么风险
  • 零基础学做网站页怎么部署wordpress
  • 网站如何做死链接提交筑站网络推广