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

做姓氏图的网站wordpress菜单字变小

做姓氏图的网站,wordpress菜单字变小,哪个网站做系统,自学服装设计下载概述 在实际开发过程中#xff0c;我们经常需要调用对方提供的接口或测试自己写的接口是否合适。 很多项目都会封装规定好本身项目的接口规范#xff0c;所以大多数需要去调用对方提供的接口或第三方接口#xff08;短信、天气等#xff09;。 在Java项目中调用第三方接口…概述 在实际开发过程中我们经常需要调用对方提供的接口或测试自己写的接口是否合适。 很多项目都会封装规定好本身项目的接口规范所以大多数需要去调用对方提供的接口或第三方接口短信、天气等。 在Java项目中调用第三方接口的方式有1.通过JDK网络类Java.net.HttpURLConnection2. 通过common封装好的HttpClient3. 通过Apache封装好的CloseableHttpClient4. 通过SpringBoot-RestTemplate Java调用第三方http接口的方式 通过JDK网络类Java.net.HttpURLConnection 比较原始的一种调用做法这里把get请求和post请求都统一放在一个方法里面。 实现过程 GET: 1、创建远程连接 2、设置连接方式get、post、put。。。 3、设置连接超时时间 4、设置响应读取时间 5、发起请求 6、获取请求数据 7、关闭连接POST: 1、创建远程连接 2、设置连接方式get、post、put。。。 3、设置连接超时时间 4、设置响应读取时间 5、当向远程服务器传送数据/写数据时需要设置为truesetDoOutput 6、当前向远程服务读取数据时设置为true该参数可有可无setDoInput 7、设置传入参数的格式:setRequestProperty 8、设置鉴权信息Authorization:setRequestProperty 9、设置参数 10、发起请求 11、获取请求数据 12、关闭连接package com.xxxx.util.common.httpConnectionUtil;import org.springframework.lang.Nullable;import java.io.*; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection;/** * author riemann * date 2019/05/24 23:42 */ public class HttpURLConnectionUtil {/*** Http get请求* param httpUrl 连接* return 响应数据*/public static String doGet(String httpUrl){//链接HttpURLConnection connection null;InputStream is null;BufferedReader br null;StringBuffer result new StringBuffer();try {//创建连接URL url new URL(httpUrl);connection (HttpURLConnection) url.openConnection();//设置请求方式connection.setRequestMethod(GET);//设置连接超时时间connection.setReadTimeout(15000);//开始连接connection.connect();//获取响应数据if (connection.getResponseCode() 200) {//获取返回的数据is connection.getInputStream();if (null ! is) {br new BufferedReader(new InputStreamReader(is, UTF-8));String temp null;while (null ! (temp br.readLine())) {result.append(temp);}}}} catch (IOException e) {e.printStackTrace();} finally {if (null ! br) {try {br.close();} catch (IOException e) {e.printStackTrace();}}if (null ! is) {try {is.close();} catch (IOException e) {e.printStackTrace();}}//关闭远程连接connection.disconnect();}return result.toString();}/*** Http post请求* param httpUrl 连接* param param 参数* return*/public static String doPost(String httpUrl, Nullable String param) {StringBuffer result new StringBuffer();//连接HttpURLConnection connection null;OutputStream os null;InputStream is null;BufferedReader br null;try {//创建连接对象URL url new URL(httpUrl);//创建连接connection (HttpURLConnection) url.openConnection();//设置请求方法connection.setRequestMethod(POST);//设置连接超时时间connection.setConnectTimeout(15000);//设置读取超时时间connection.setReadTimeout(15000);//DoOutput设置是否向httpUrlConnection输出DoInput设置是否从httpUrlConnection读入此外发送post请求必须设置这两个//设置是否可读取connection.setDoOutput(true);connection.setDoInput(true);//设置通用的请求属性connection.setRequestProperty(accept, */*);connection.setRequestProperty(connection, Keep-Alive);connection.setRequestProperty(user-agent, Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1));connection.setRequestProperty(Content-Type, application/json;charsetutf-8);//拼装参数if (null ! param param.equals()) {//设置参数os connection.getOutputStream();//拼装参数os.write(param.getBytes(UTF-8));}//设置权限//设置请求头等//开启连接//connection.connect();//读取响应if (connection.getResponseCode() 200) {is connection.getInputStream();if (null ! is) {br new BufferedReader(new InputStreamReader(is, GBK));String temp null;while (null ! (temp br.readLine())) {result.append(temp);result.append(\r\n);}}}} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {//关闭连接if(br!null){try {br.close();} catch (IOException e) {e.printStackTrace();}}if(os!null){try {os.close();} catch (IOException e) {e.printStackTrace();}}if(is!null){try {is.close();} catch (IOException e) {e.printStackTrace();}}//关闭连接connection.disconnect();}return result.toString();}public static void main(String[] args) {String message doPost(https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel13026194071, );System.out.println(message);} }通过apache common封装好的HttpClient httpClient的get或post请求方式步骤 .生成一个HttpClient对象并设置相应的参数 .生成一个GetMethod对象或PostMethod并设置响应的参数 .用HttpClient生成的对象来执行GetMethod生成的Get方法 .处理响应状态码 .若响应正常处理HTTP响应内容 .释放连接。导入如下jar包 !--HttpClient-- dependencygroupIdcommons-httpclient/groupIdartifactIdcommons-httpclient/artifactIdversion3.1/version /dependency!--fastjson-- dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.32/version /dependencypackage com.riemann.springbootdemo.util.common.httpConnectionUtil;import com.alibaba.fastjson.JSONObject; import org.apache.commons.httpclient.*; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams;import java.io.IOException;/*** author riemann* date 2019/05/25 0:58*/ public class HttpClientUtil {/*** httpClient的get请求方式* 使用GetMethod来访问一个URL对应的网页实现步骤* 1.生成一个HttpClient对象并设置相应的参数* 2.生成一个GetMethod对象并设置响应的参数* 3.用HttpClient生成的对象来执行GetMethod生成的Get方法* 4.处理响应状态码* 5.若响应正常处理HTTP响应内容* 6.释放连接。* param url* param charset* return*/public static String doGet(String url, String charset) {//1.生成HttpClient对象并设置参数HttpClient httpClient new HttpClient();//设置Http连接超时为5秒httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);//2.生成GetMethod对象并设置参数GetMethod getMethod new GetMethod(url);//设置get请求超时为5秒getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);//设置请求重试处理用的是默认的重试处理请求三次getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());String response ;//3.执行HTTP GET 请求try {int statusCode httpClient.executeMethod(getMethod);//4.判断访问的状态码if (statusCode ! HttpStatus.SC_OK) {System.err.println(请求出错 getMethod.getStatusLine());}//5.处理HTTP响应内容//HTTP响应头部信息这里简单打印Header[] headers getMethod.getResponseHeaders();for(Header h : headers) {System.out.println(h.getName() --------------- h.getValue());}//读取HTTP响应内容这里简单打印网页内容//读取为字节数组byte[] responseBody getMethod.getResponseBody();response new String(responseBody, charset);System.out.println(-----------response: response);//读取为InputStream在网页内容数据量大时候推荐使用//InputStream response getMethod.getResponseBodyAsStream();} catch (HttpException e) {//发生致命的异常可能是协议不对或者返回的内容有问题System.out.println(请检查输入的URL!);e.printStackTrace();} catch (IOException e) {//发生网络异常System.out.println(发生网络异常!);} finally {//6.释放连接getMethod.releaseConnection();}return response;}/*** post请求* param url* param json* return*/public static String doPost(String url, JSONObject json){HttpClient httpClient new HttpClient();PostMethod postMethod new PostMethod(url);postMethod.addRequestHeader(accept, */*);postMethod.addRequestHeader(connection, Keep-Alive);//设置json格式传送postMethod.addRequestHeader(Content-Type, application/json;charsetGBK);//必须设置下面这个HeaderpostMethod.addRequestHeader(User-Agent, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36);//添加请求参数postMethod.addParameter(commentId, json.getString(commentId));String res ;try {int code httpClient.executeMethod(postMethod);if (code 200){res postMethod.getResponseBodyAsString();System.out.println(res);}} catch (IOException e) {e.printStackTrace();}return res;}public static void main(String[] args) {System.out.println(doGet(http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel13026194071, GBK));System.out.println(-----------分割线------------);System.out.println(-----------分割线------------);System.out.println(-----------分割线------------);JSONObject jsonObject new JSONObject();jsonObject.put(commentId, 13026194071);System.out.println(doPost(http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel13026194071, jsonObject));} }通过Apache封装好的CloseableHttpClient CloseableHttpClient是在HttpClient的基础上修改更新而来的这里还涉及到请求头token的设置请求验证利用fastjson转换请求或返回结果字符串为json格式当然上面两种方式也是可以设置请求头token、json的这里只在下面说明。 导入如下jar包 !--CloseableHttpClient-- dependencygroupIdorg.apache.httpcomponents/groupIdartifactIdhttpclient/artifactIdversion4.5.2/version /dependency!--fastjson-- dependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.32/version /dependencypackage com.riemann.springbootdemo.util.common.httpConnectionUtil;import com.alibaba.fastjson.JSONObject; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; 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.HttpClientBuilder; import org.apache.http.util.EntityUtils;import java.io.IOException; import java.io.UnsupportedEncodingException;/*** author riemann* date 2019/05/25 1:35*/ public class CloseableHttpClientUtil {private static String tokenString ;private static String AUTH_TOKEN_EXPIRED AUTH_TOKEN_EXPIRED;private static CloseableHttpClient httpClient null;/*** 以get方式调用第三方接口* param url* param token* return*/public static String doGet(String url, String token) {//创建HttpClient对象CloseableHttpClient httpClient HttpClientBuilder.create().build();HttpGet httpGet new HttpGet(url);if (null ! tokenString !tokenString.equals()) {tokenString getToken();}//api_gateway_auth_token自定义header头用于token验证使用httpGet.addHeader(api_gateway_auth_token,tokenString);httpGet.addHeader(User-Agent, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36);try {HttpResponse response httpClient.execute(httpGet);if (response.getStatusLine().getStatusCode() HttpStatus.SC_OK) {//返回json格式String res EntityUtils.toString(response.getEntity());return res;}} catch (IOException e) {e.printStackTrace();}return null;}/*** 以post方式调用第三方接口* param url* param json* return*/public static String doPost(String url, JSONObject json) {if (null httpClient) {httpClient HttpClientBuilder.create().build();}HttpPost httpPost new HttpPost(url);if (null ! tokenString tokenString.equals()) {tokenString getToken();}//api_gateway_auth_token自定义header头用于token验证使用httpPost.addHeader(api_gateway_auth_token, tokenString);httpPost.addHeader(User-Agent, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36);try {StringEntity se new StringEntity(json.toString());se.setContentEncoding(UTF-8);//发送json数据需要设置contentTypese.setContentType(application/x-www-form-urlencoded);//设置请求参数httpPost.setEntity(se);HttpResponse response httpClient.execute(httpPost);if (response.getStatusLine().getStatusCode() HttpStatus.SC_OK) {//返回json格式String res EntityUtils.toString(response.getEntity());return res;}} catch (IOException e) {e.printStackTrace();} finally {if (httpClient ! null){try {httpClient.close();} catch (IOException e) {e.printStackTrace();}}}return null;}/*** 获取第三方接口的token*/public static String getToken() {String token ;JSONObject object new JSONObject();object.put(appid, appid);object.put(secretkey, secretkey);if (null httpClient) {httpClient HttpClientBuilder.create().build();}HttpPost httpPost new HttpPost(http://localhost/login);httpPost.addHeader(User-Agent, Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36);try {StringEntity se new StringEntity(object.toString());se.setContentEncoding(UTF-8);//发送json数据需要设置contentTypese.setContentType(application/x-www-form-urlencoded);//设置请求参数httpPost.setEntity(se);HttpResponse response httpClient.execute(httpPost);//这里可以把返回的结果按照自定义的返回数据结果把string转换成自定义类//ResultTokenBO result JSONObject.parseObject(response, ResultTokenBO.class);//把response转为jsonObjectJSONObject result (JSONObject) JSONObject.parseObject(String.valueOf(response));if (result.containsKey(token)) {token result.getString(token);}} catch (IOException e) {e.printStackTrace();}return token;}/*** 测试*/public static void test(String telephone) {JSONObject object new JSONObject();object.put(telephone, telephone);//首先获取tokentokenString getToken();String response doPost(http://localhost/searchUrl, object);//如果返回的结果是list形式的需要使用JSONObject.parseArray转换//ListResult list JSONObject.parseArray(response, Result.class);System.out.println(response);}public static void main(String[] args) {test(12345678910);} }通过okhttp 应大家的响应okhttp 现在也是蛮流行的基于手机端很火这里分享一下OkHttpClient客户端业务代码get、post请求直接调用就好哈。 pom文件引入依赖包 dependencygroupIdcom.squareup.okhttp3/groupIdartifactIdokhttp/artifactIdversion3.10.0/version /dependencySlf4j public class OkHttpClient {private static final MediaType JSON MediaType.parse(application/json; charsetutf-8);private volatile static okhttp3.OkHttpClient client;private static final int MAX_IDLE_CONNECTION Integer.parseInt(ConfigManager.get(httpclient.max_idle_connection));private static final long KEEP_ALIVE_DURATION Long.parseLong(ConfigManager.get(httpclient.keep_alive_duration));private static final long CONNECT_TIMEOUT Long.parseLong(ConfigManager.get(httpclient.connectTimeout));private static final long READ_TIMEOUT Long.parseLong(ConfigManager.get(httpclient. ));/*** 单例模式(双重检查模式) 获取类实例** return client*/private static okhttp3.OkHttpClient getInstance() {if (client null) {synchronized (okhttp3.OkHttpClient.class) {if (client null) {client new okhttp3.OkHttpClient.Builder().connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS).readTimeout(READ_TIMEOUT, TimeUnit.SECONDS).connectionPool(new ConnectionPool(MAX_IDLE_CONNECTION, KEEP_ALIVE_DURATION,TimeUnit.MINUTES)).build();}}}return client;}public static String syncPost(String url, String json) throws IOException {RequestBody body RequestBody.create(JSON, json);Request request new Request.Builder().url(url).post(body).build();try {Response response OkHttpClient.getInstance().newCall(request).execute();if (response.isSuccessful()) {String result response.body().string();log.info(syncPost response {}, responseBody {}, response, result);return result;}String result response.body().string();log.info(syncPost response {}, responseBody {}, response, result);throw new IOException(三方接口返回http状态码为 response.code());} catch (Exception e) {log.error(syncPost() url:{} have a ecxeption {}, url, e);throw new RuntimeException(syncPost() have a ecxeption {} e.getMessage());}}public static String syncGet(String url, MapString, Object headParamsMap) throws IOException {Request request;final Request.Builder builder new Request.Builder().url(url);try {if (!CollectionUtils.isEmpty(headParamsMap)) {final IteratorMap.EntryString, Object iterator headParamsMap.entrySet().iterator();while (iterator.hasNext()) {final Map.EntryString, Object entry iterator.next();builder.addHeader(entry.getKey(), (String) entry.getValue());}}request builder.build();Response response OkHttpClient.getInstance().newCall(request).execute();String result response.body().string();log.info(syncGet response {},responseBody {}, response, result);if (!response.isSuccessful()) {throw new IOException(三方接口返回http状态码为 response.code());}return result;} catch (Exception e) {log.error(remote interface url:{} have a ecxeption {}, url, e);throw new RuntimeException(三方接口返回异常);}}}
http://www.pierceye.com/news/770568/

相关文章:

  • 做与食品安全有关的网站徐州企业网站设计
  • 番禺网站建设策划江阴市建设局官网站
  • 建设网站模块需要哪些内容石家庄城乡建设厅网站
  • 公司网站后台管理网络公司名字大全三字
  • 广西住房建设厅网站广州seo工作
  • 做分销商城网站的wordpress 知更鸟 网格
  • 推销商务网站的途径有哪些爱网站查询挖掘工具
  • 苏州现代建设公司网站备案的域名做电影网站
  • 长沙seo网站优化公司wordpress5.1下载
  • 七星彩网投网站建设鹤壁公司做网站
  • 多语言企业网站建设费用怎么自己做购物网站
  • 中国网站排名前100线上网站开发相关书籍
  • 网站制作图书网站建设指南
  • 网站备案简单吗优化关键词排名软件
  • 泉山网站开发安徽建设工程造价信息网
  • 如何使用电子商务网站做seo需要用到什么软件
  • 新乡商城网站建设哪家专业潮汕学院网站开发
  • 西安响应式网站开发网站空间多少钱一年
  • 做电子相册的大网站怎样提高网站的权重
  • seo网站设计外包去哪个网站有客户找做标书的
  • 微商招商网站源码互联网营销推广方案
  • 深圳做网站服务公司河北石家庄最新新闻
  • 山东济南seo整站优化唐山网站建设那家性价比高
  • c 可以做哪些网站小说网站建设采集
  • 公司网站备案条件高校网站集群平台子站开发
  • 制作网站能赚钱吗单位发购物或电影卡有哪些app
  • 我们网站在那里登陆后台系统管理网站建设服务咨询
  • 免费上传图片的网址网站seo工作内容
  • chatgpt 网站一对一直播软件开发
  • 网站做排行多少费用个人电脑做网站打不开数据库