首页 > 编程知识 正文

构造方法通过什么调用,分析程序的运行结果

时间:2023-05-06 02:05:18 阅读:169013 作者:2069

//超时时间10s static int timeout=10000; /**上下文处理http和https调用--get请求* */publicstaticstringdogetwithauthorization (/临时变量stringURL, String authorization临时变量string if(temp.indexof(https )-1 ) returndohttpsgetwithauthorization (URL,authorization ) } else { returndohttpgetwithauthorization (URL,authorization ); } } /** *查看情况并处理http和https调用--post请求* */publicstaticstringdopostwithcontenttype (string URL,String paramBody,https ) if(temp.indexof(https )-1 ) returndohttpspostwithconttype (URL,paramBody,contenttype ); } else { returndohttppostwithcontenttype (URL,paramBody,contenttype ); } } /** *https访问--post请求* */publicstaticstringdohttpspostwithcontenttype (string URL,String paramBody, 字符串内容类型(logger.info ) httpspost发送URL:'URL )、请求参数:'parambody )、内容类型3360 ' content type ) ) //第一个步骤: HttpClient对象closeablehttpclienthttpclient=createsslclientdefault (; 字符串返回值=null; responsehandlerstringresponsehandler=newbasicresponsehandler (; try{ //第二步骤:创建httpPost对象http post=new http post (URL ); //超时时间为10秒,如果超时没有响应,则返回requestconfigrequestconfig=request config.custom (.setconnecttimeout ).setconnectionrtion //在步骤httpPost中输入参数和格式stringentityrequestentity=new string entity (param body,' utf-8 ' ); request entity.setcontentencoding (utf-8 ); 如post提交' application/json '或表单提交' application/x-www-form-urlencoded ' http post.setheader (content ) //步骤发送HttpPost请求,并获取返回值return value=http client.execute (http post,responseHandler ); logger.info(httpspost接收URL: ) URL )、请求参数: ) parambody )、返回值: ) returnvalue ); }catch(exceptione ) { e.printStackTrace ); logger.info(httpspost异常,url:"+url+",请求参数:"+paramBody+",异常信息:"+e.toString()); } finally { close(httpClient); } //第五步:处理返回值 return returnValue; } /** *http访问--post请求 **/ public static String doHttpPostWithContentType(String url,String paramBody,String contentType){ logger.info("Httppost发送url:"+url+",请求参数:"+paramBody+",ContentType:"+contentType); CloseableHttpClient httpClient=null; String returnValue=null;// CloseableHttpResponse response = null; try{ //第一步:创建HttpClient对象 httpClient = HttpClients.createDefault(); ResponseHandler<String> responseHandler = new BasicResponseHandler(); HttpPost httpPost = new HttpPost(url); //超时时间,超时无响应会报错 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build(); httpPost.setConfig(requestConfig); //第三步:给httpPost设置参数及格式 StringEntity requestEntity = new StringEntity(paramBody,"utf-8"); requestEntity.setContentEncoding("UTF-8"); //设置提交内容方式,如post提交 "application/json",或表单提交"application/x-www-form-urlencoded" httpPost.setHeader("Content-type", contentType); httpPost.setEntity(requestEntity); //第四步:发送HttpPost请求,获取返回值 returnValue = httpClient.execute(httpPost,responseHandler); logger.info("Httppost接收url:"+url+",请求参数:"+paramBody+",ContentType:"+contentType+",返回值:"+returnValue); }catch (Exception e){ e.printStackTrace(); logger.info("Httppost异常,url:"+url+",请求参数:"+paramBody+",ContentType:"+contentType+",异常信息:"+e.toString()); }finally { close(httpClient); } return returnValue; } /** * https访问--get请求 **/ public static String doHttpsGetWithAuthorization(String url,String authorization){ logger.info("HttpsGet发送url:"+url); // 1. 创建带ssl的Httpclient对象 CloseableHttpClient httpClient = createSSLClientDefault(); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String returnValue=null; try { // 2. 创建Http Get请求 HttpGet httpGet = new HttpGet(url); //超时时间30秒,超时无响应会报错 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build(); httpGet.setConfig(requestConfig); //设置鉴权信息 httpGet.setHeader("Authorization",authorization); returnValue = httpClient.execute(httpGet,responseHandler); logger.info("HttpsGet接收url:"+url+",返回值:"+returnValue); } catch (Exception e) { e.printStackTrace(); logger.info("HttpsGet异常,url:"+url+",异常信息:"+e.toString()); } finally { close(httpClient); } return returnValue; } /** * http访问--get请求 **/ public static String doHttpGetWithAuthorization(String url,String authorization){ logger.info("HttpGet发送url:"+url); CloseableHttpClient httpClient = HttpClients.createDefault(); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String returnValue=null; CloseableHttpResponse response = null; try { // 2. 创建Http Get请求 HttpGet httpGet = new HttpGet(url); //超时时间,超时无响应会报错 RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build(); httpGet.setConfig(requestConfig); //设置鉴权信息 httpGet.setHeader("Authorization",authorization); // 3. 执行http请求 returnValue = httpClient.execute(httpGet,responseHandler); logger.info("HttpGet接收url:"+url+",返回值:"+returnValue); } catch (Exception e) { e.printStackTrace(); logger.info("HttpGet异常,url:"+url+",返回值:"+returnValue+",异常信息:"+e.toString()); } finally { close(httpClient); } return returnValue; } /** *SSL身份认证 **/ public static CloseableHttpClient createSSLClientDefault() { CloseableHttpClient closeableHttpClient; try { SSLContext sslContext = new org.apache.http.ssl.SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); closeableHttpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build(); logger.info("SSLHttpClient创建成功"); return closeableHttpClient; } catch (KeyManagementException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } return null; } public static void close(CloseableHttpClient httpClient){ try { httpClient.close(); } catch (IOException e) { e.printStackTrace(); logger.info("http关闭异常"); } }

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。