2.3.6. fejezet, HTTPClient kommunikáció HTTPS protokolon keresztül

A HTTPS kapcsolat kiépítéséhez használjuk az Apache HTTPClient csomagját. Ha a Tomcat trustStore paraméterét helyesen beállítottuk a telepítés során, a letöltés a következő módon történhet:

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
 
		Protocol https = new Protocol("https", (ProtocolSocketFactory)(new SSLProtocolSocketFactory()),
				443);
		HttpClient client = new HttpClient();
		client.getHostConfiguration().setHost("localhost", 443, https);
		request.setCharacterEncoding(defaultForwardCharset);
		ByteArrayOutputStream ostream = new ByteArrayOutputStream();
		if (logger!=null)
			logger.log(Level.INFO,URLDecoder.decode(request.getQueryString(),defaultForwardCharset));
		OutputStreamWriter sout = new OutputStreamWriter(ostream, defaultBackwardCharset);
		String url = request.getParameter("url");
		if (url != null) {
			response.setCharacterEncoding(defaultForwardCharset);
 
			try {
				String[] params = url.split("\\|");
				String[] outParams = new String[params.length-1];
				for (int i = 1; i < params.length; i++) {
					String[] item = params[i].split("=");
					if (item.length==2) {
					  item[1] = URLEncoder.encode(item[1],defaultForwardCharset);
					  outParams[i-1] = item[0] + "=" + item[1];
					} else
					  outParams[i-1] = item[0] + "=";	
				}
 
				url = params[0] + "?" + StrTK.join("&", outParams);
				if (logger!=null)
				  logger.log(Level.INFO, URLDecoder.decode(url,defaultBackwardCharset));
				HttpMethodParams methodParams = new HttpMethodParams(); 
				methodParams.setContentCharset(defaultBackwardCharset);
				GetMethod httpget = new GetMethod(url);
				httpget.setParams(methodParams);
				client.executeMethod(httpget);
				try {
					String content;
					if (httpget.getStatusCode() == 200)
						content = httpget.getResponseBodyAsString();
					else
						content = "1";
					if (content != null) {
						sout.write(content);
						sout.flush();
					}
				} finally {
					httpget.releaseConnection();
				}
			} catch (UnsupportedEncodingException e) {
				sout.write("1");
				sout.flush();
				e.printStackTrace();
			} catch (IOException e) {
				sout.write("1");
				sout.flush();
				e.printStackTrace();
			}
			ostream.writeTo(response.getOutputStream());
		}
	}

Ha nem állítottuk be a szerveren a trustStore paramétert, akkor kódból kell megtennünk a Security.addProvider hívás előtt.

		String trustStorePath = getServletContext().getRealPath("/WEB-INF/.keystore");
		System.setProperty("javax.net.ssl.trustStore", trustStorePath);
		System.setProperty("javax.net.ssl.trustStorePassword", "storepass");