protected void callHttp() throws IOException { if ("POST".equals(this.method.toUpperCase())) { String url = HttpClientUtil.getURL(this.reqContent); String queryString = HttpClientUtil.getQueryString(this.reqContent); byte[] postData = queryString.getBytes(this.charset); this.httpPostMethod(url, postData); return; } this.httpGetMethod(this.reqContent); }
/** * post��ʽ���� * * @param conn * @param postData * @throws IOException */ protected void doPost(HttpURLConnection conn, byte[] postData) throws IOException { // ��post��ʽͨ�� conn.setRequestMethod("POST"); // ��������Ĭ������ this.setHttpRequest(conn); // Content-Type conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream()); final int len = 1024; // 1KB HttpClientUtil.doOutput(out, postData, len); // �ر��� out.close(); // ��ȡ��Ӧ����״̬�� this.responseCode = conn.getResponseCode(); // ��ȡӦ�������� this.inputStream = conn.getInputStream(); }
/** * ��https get��ʽͨ�� * * @param url * @param sslContext * @throws IOException */ protected void httpsGetMethod(String url, SSLContext sslContext) throws IOException { SSLSocketFactory sf = sslContext.getSocketFactory(); HttpsURLConnection conn = HttpClientUtil.getHttpsURLConnection(url); conn.setSSLSocketFactory(sf); this.doGet(conn); }
protected void callHttps() throws IOException, CertificateException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException, KeyManagementException { // caĿ¼ String caPath = this.caFile.getParent(); File jksCAFile = new File(caPath + "/" + TenpayHttpClient.JKS_CA_FILENAME); if (!jksCAFile.isFile()) { X509Certificate cert = (X509Certificate) HttpClientUtil.getCertificate(this.caFile); FileOutputStream out = new FileOutputStream(jksCAFile); // store jks file HttpClientUtil.storeCACert( cert, TenpayHttpClient.JKS_CA_ALIAS, TenpayHttpClient.JKS_CA_PASSWORD, out); out.close(); } FileInputStream trustStream = new FileInputStream(jksCAFile); FileInputStream keyStream = new FileInputStream(this.certFile); SSLContext sslContext = HttpClientUtil.getSSLContext( trustStream, TenpayHttpClient.JKS_CA_PASSWORD, keyStream, this.certPasswd); // �ر��� keyStream.close(); trustStream.close(); if ("POST".equals(this.method.toUpperCase())) { String url = HttpClientUtil.getURL(this.reqContent); String queryString = HttpClientUtil.getQueryString(this.reqContent); byte[] postData = queryString.getBytes(this.charset); this.httpsPostMethod(url, postData, sslContext); return; } this.httpsGetMethod(this.reqContent, sslContext); }
protected void httpsPostMethod(String url, byte[] postData, SSLContext sslContext) throws IOException { SSLSocketFactory sf = sslContext.getSocketFactory(); HttpsURLConnection conn = HttpClientUtil.getHttpsURLConnection(url); conn.setSSLSocketFactory(sf); this.doPost(conn, postData); }
/** * ��http get��ʽͨ�� * * @param url * @throws IOException */ protected void httpGetMethod(String url) throws IOException { HttpURLConnection httpConnection = HttpClientUtil.getHttpURLConnection(url); this.setHttpRequest(httpConnection); httpConnection.setRequestMethod("GET"); this.responseCode = httpConnection.getResponseCode(); this.inputStream = httpConnection.getInputStream(); }
/** * ����Ӧ�� * * @throws IOException */ protected void doResponse() throws IOException { if (null == this.inputStream) { return; } BufferedReader reader = new BufferedReader(new InputStreamReader(this.inputStream, this.charset)); // ��ȡӦ������ this.resContent = HttpClientUtil.bufferedReader2String(reader); // �ر��� reader.close(); // �ر������� this.inputStream.close(); }
/** * ��http post��ʽͨ�� * * @param url * @param postData * @throws IOException */ protected void httpPostMethod(String url, byte[] postData) throws IOException { HttpURLConnection conn = HttpClientUtil.getHttpURLConnection(url); this.doPost(conn, postData); }