doGet,doPost与doPut读取方法

网友投稿 672 2022-09-03

doGet,doPost与doPut读取方法

doGet,doPost与doPut读取方法

1.设置参数,使用协议读取static void paramSettings(HttpClient try { //Secure Protocol implementation. SSLContext ctx = SSLContext.getInstance("SSL"); //Implementation of a trust manager for X509 certificates X509TrustManager tm = new X509TrustManager() { public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }; X509HostnameVerifier hostnameVerifier = new X509HostnameVerifier(){ public boolean verify(String hostname, SSLSession session) { // TODO Auto-generated method stub return true; } public void verify(String arg0, SSLSocket arg1) throws IOException { // TODO Auto-generated method stub } public void verify(String arg0, X509Certificate arg1) throws SSLException { // TODO Auto-generated method stub } public void verify(String arg0, String[] arg1, String[] arg2) throws SSLException { // TODO Auto-generated method stub } }; ctx.init(null, new TrustManager[] { tm }, null); SSLSocketFactory ssf = new SSLSocketFactory(ctx,hostnameVerifier); ClientConnectionManager ccm = //register protocol in scheme registry SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("443, ssf)); sr.register(new Scheme("80,PlainSocketFactory .getSocketFactory())); //set Time out TIME_OUT); TIME_OUT); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } }

2.doGet

public static String getHTTPSjsonRequest(String url, String userName, String password) { logger.info("HttpsUtil : getHTTPSResult start............"); String result = null; try { HttpClient = getHttpClient(); HttpGet = new HttpGet(url); String authString = userName + ":" + password; String authStringEnc = new String(Base64.encode(authString .getBytes())); "Basic " + authStringEnc); "application/json"); ResponseHandler responseHandler = new BasicResponseHandler(); result = responseHandler); logger.debug("HttpsUtil : getHTTPSResult;result = " + result); // Create a response handler } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } logger.info("HttpsUtil : getHTTPSResult end........"); return result; }

3.doPut

public static String putHTTPSJsonRequest(String url) { logger.info("HttpsUtil : putHTTPSResult start............"); logger.debug("url : " + url); String info = null; HttpClient = getHttpClient(); HttpPut = new HttpPut(url); try { HttpResponse = HttpEntity entity = info = EntityUtils.toString(entity, "UTF-8"); System.out.println("info = " + info); "application/json"); } catch (Exception e) { logger.debug("putData Exception url:{}", url, e); } finally { } logger.info("HttpsUtil : putHTTPSResult end........"); // return info; return info; }

4.doPost

public static String postHTTPSJsonRequest(String url, Map params) { logger.info("HttpsUtil : postHTTPSResult start............"); logger.debug("url : " + url); String response = null; String info = null; HttpClient = getHttpClient(); HttpPost = new HttpPost(url); try { List nameValuePairs = new ArrayList(); if (params != null && params.size() > 0) { Iterator keysIterator = params.keySet().iterator(); while (keysIterator.hasNext()) { String key = (String) keysIterator.next(); String value = params.get(key); nameValuePairs.add(new BasicNameValuePair(key, value)); } } if (nameValuePairs != null && nameValuePairs.size() > 0) { UrlEncodedFormEntity(nameValuePairs, "UTF-8")); } HttpResponse = HttpEntity entity = info = EntityUtils.toString(entity, "UTF-8"); System.out.println("info = " + info); "application/json"); } catch (Exception e) { logger.debug("putData Exception url:{}", url, e); } finally { } logger.info("HttpsUtil : postHTTPSResult end........"); return info; }

public static HttpClient getHttpClientWithSSL(String SSLPath) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException, UnrecoverableKeyException{ HttpClient = new DefaultHttpClient();KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File(SSLPath)); try { trustStore.load(instream, "123456".toCharArray()); } finally { instream.close(); } SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore,"123456",trustStore); Scheme sch = new Scheme("socketFactory, 443); // return }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:nginx实现一个域名配置多个laravel项目
下一篇:dom4j解释XML
相关文章

 发表评论

暂时没有评论,来抢沙发吧~