1.设置参数,使用协议读取https public static void paramSettings(HttpClient 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 = httpclient.getConnectionManager(); //register https protocol in httpclient's scheme registry SchemeRegistry sr = ccm.getSchemeRegistry(); sr.register(new Scheme("https", 443, ssf)); sr.register(new Scheme("http", 80,PlainSocketFactory .getSocketFactory())); //set Time out httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT); httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 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 httpclient = getHttpClient(); HttpGet httpget = new HttpGet(url); String authString = userName + ":" + password; String authStringEnc = new String(Base64.encode(authString .getBytes())); httpget.addHeader("Authorization", "Basic " + authStringEnc); httpget.addHeader("content-type", "application/json"); ResponseHandler responseHandler = new BasicResponseHandler(); result = httpclient.execute(httpget, responseHandler); logger.debug("HttpsUtil : getHTTPSResult;result = " + result); // Create a response handler httpget.releaseConnection(); } 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 httpclient = getHttpClient(); HttpPut httpPut = new HttpPut(url); try { HttpResponse httpresponse = httpclient.execute(httpPut); HttpEntity entity = httpresponse.getEntity(); info = EntityUtils.toString(entity, "UTF-8"); System.out.println("info = " + info); httpPut.addHeader("content-type", "application/json"); } catch (Exception e) { logger.debug("putData Exception url:{}", url, e); } finally { httpPut.releaseConnection(); } logger.info("HttpsUtil : putHTTPSResult end........"); // return info; return info; }
4.doPost
public static String postHTTPSJsonRequest(String url, Map<String, String> params) { logger.info("HttpsUtil : postHTTPSResult start............"); logger.debug("url : " + url); String response = null; String info = null; HttpClient httpclient = getHttpClient(); HttpPost httpPost = new HttpPost(url); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 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) { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); } HttpResponse httpresponse = httpclient.execute(httpPost); HttpEntity entity = httpresponse.getEntity(); info = EntityUtils.toString(entity, "UTF-8"); System.out.println("info = " + info); httpPost.addHeader("content-type", "application/json"); } catch (Exception e) { logger.debug("putData Exception url:{}", url, e); } finally { httpPost.releaseConnection(); } logger.info("HttpsUtil : postHTTPSResult end........"); return info; }
public static HttpClient getHttpClientWithSSL(String SSLPath) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, KeyManagementException, UnrecoverableKeyException{ HttpClient 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("https", socketFactory, 443); httpclient.getConnectionManager().getSchemeRegistry().register(sch); // httpclient.getConnectionManager().shutdown(); return httpclient; }
相关推荐
doGet与doPost之间的区别doGet与doPost之间的区别doGet与doPost之间的区别doGet与doPost之间的区别
简洁明了说明doget和dopost的区别
doget dopost区别 doget dopost区别 doget dopost区别
doget 可以打印出网页,在文本框中输入数字和字母 dopost接受数据并在根目录查看和下载文件。
Servlet中doGet与doPost的区别
Servlet访问流程以及doGet和doPost 能力目标 能叙述Servlet访问流程以及Servlet的生命周期 能描述init()、service()、destory()什么时候被调用的 能描述doGet()和doPost()内部访问流程
模拟http请求实现文件上传,模拟doGet、doPost请求,实现信息发送
测试助手(还有很多事情要添加,但是现在应该涵盖了测试doPost的基本知识) 这里的想法是直接测试Java Servlet的doPost,doGet,doPut,doDelete。为什么? 尽管您仍然必须模拟某些服务和DAO类(我曾使用PowerMock)...
主要介绍了Java doGet, doPost方法和文件上传实例代码的相关资料,需要的朋友可以参考下
doget和dopost中文乱码问题1
c++通过doget和dopost的方式连接http的类,本人经常使用可靠
这个是servlet中的doGet()和doPost()的区别,介绍的是相当的详细。值得期待。
自己写的用于Servlet转码 charset包下charset类里的charset方法传入参数,返回经过utf-8转码的信息 注:主要用于Servlet中的doGet方法
我们在javaWeb项目时,使用doget和dopost总是会出现各种原因的中文乱码问题,楼主在查阅大量资料后,将为什么有这样的问题,和如何解决这种问题做个总结。 思维导图: 1. 应答乱码处理(response输出页面时乱码) 1.1 ...
Servlet中doGet()测试方法,方便测试Servlet中数据库的查询功能
对于每个HTTP方法,都对应一个HTTPServlet方法,doGet,doPost,doPut等。就像在Java中一样。 virtual void doGet(HTTPRequest &request, HTTPResponse &response); virtual void doPost(HTTPRequest &request, ...
####spring-httpclient-helper是将HttpClient与Spring整合的一个工具,HttpClient的一个帮助,对外只暴露一个多例的HttpClientApiService,该类封装了doGet和doPost等常用的Http请求方式使用了HttpClient的连接池...
doget dopost 的区别 el表达式的使用 requestDispatcher 和 location的区别 jsp内置对象的大小
Servlet中的doGet()方法默认是用ISO-8819-1显示的,这个字符集不支持中文,而且过滤器对doGet()无效,也不能通过一般方式修改,要想用doGet()显示中文必须通过设置tomcat的字符集才行