标签:
1 package action; 2 3 import java.io.BufferedReader; 4 import java.io.ByteArrayOutputStream; 5 import java.io.File; 6 import java.io.FileInputStream; 7 import java.io.FileNotFoundException; 8 import java.io.IOException; 9 import java.io.InputStream; 10 import java.io.InputStreamReader; 11 import java.io.OutputStream; 12 import java.io.PrintWriter; 13 import java.net.URL; 14 import java.net.URLConnection; 15 import java.util.HashMap; 16 import java.util.Map; 17 import java.util.List; 18 import java.security.KeyStore; 19 import java.security.KeyStoreException; 20 import java.security.NoSuchAlgorithmException; 21 import java.security.UnrecoverableKeyException; 22 import java.security.cert.CertificateException; 23 24 import javax.net.ssl.HttpsURLConnection; 25 import javax.net.ssl.KeyManagerFactory; 26 import javax.net.ssl.SSLContext; 27 import javax.net.ssl.SSLSocketFactory; 28 import javax.net.ssl.TrustManager; 29 30 import com.bill99.mgw.entity.*; 31 import com.bill99.mgw.util.*; 32 33 public class Post 34 { 35 /** 36 * 向指定URL发送POST方法的请求 37 * @param url 发送请求的URL 38 * @param param 请求参数,请求参数应该是name1=value1&name2=value2的形式。 39 * @return URL所代表远程资源的响应 40 * @throws Exception 41 */ 42 public static HashMap sendPost(String url,String tr1XML) throws Exception 43 { 44 OutputStream out = null; 45 HashMap respXml = null; 46 47 //获取证书路径 48 File certFile = new File("C://TS/10411004511201290.jks"); 49 //File certFile = new File("D://hui.ding/workspace/DH_QuickPay/src/10411004511201290.jks"); 50 //访问Java密钥库,JKS是keytool创建的Java密钥库,保存密钥。 51 KeyStore ks = KeyStore.getInstance("JKS"); 52 ks.load(new FileInputStream(certFile), "vpos123".toCharArray()); 53 //创建用于管理JKS密钥库的密钥管理器 54 KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 55 //初始化证书 56 kmf.init(ks, "vpos123".toCharArray()); 57 58 //同位体验证信任决策源//同位体验证可信任的证书来源 59 TrustManager[] tm = { new MyX509TrustManager() }; 60 61 //初始化安全套接字 62 SSLContext sslContext = SSLContext.getInstance("SSL"); 63 //初始化SSL环境。第二个参数是告诉JSSE使用的可信任证书的来源,设置为null是从javax.net.ssl.trustStore中获得证书。 64 //第三个参数是JSSE生成的随机数,这个参数将影响系统的安全性,设置为null是个好选择,可以保证JSSE的安全性。 65 sslContext.init(kmf.getKeyManagers(),tm, null); 66 67 //根据上面配置的SSL上下文来产生SSLSocketFactory,与通常的产生方法不同 68 SSLSocketFactory factory = sslContext.getSocketFactory(); 69 70 try 71 { 72 URL realUrl = new URL(url); 73 //打开和URL之间的连接 74 HttpsURLConnection conn = (HttpsURLConnection)realUrl.openConnection(); 75 //创建安全的连接套接字 76 conn.setSSLSocketFactory(factory); 77 //发送POST请求必须设置如下两行,使用 URL 连接进行输出、入 78 conn.setDoOutput(true); 79 conn.setDoInput(true); 80 //设置URL连接的超时时限 81 conn.setReadTimeout(100000); 82 83 //设置通用的请求属性 84 String authString = "104110045112012" + ":" + "vpos123"; 85 String auth = "Basic " + Base64Binrary.encodeBase64Binrary(authString.getBytes()); 86 conn.setRequestProperty("Authorization", auth); 87 88 // 获取URLConnection对象对应的输出流 89 out = conn.getOutputStream(); 90 //发送请求参数 91 out.write(tr1XML.getBytes("utf-8")); 92 //flush 输出流的缓冲 93 out.flush(); 94 95 //得到服务端返回 96 InputStream is = conn.getInputStream(); 97 String reqData = ""; 98 if(is!=null && !"".equals(is)) 99 { 100 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 101 byte[] receiveBuffer = new byte[2048];//缓冲区长度 102 int readBytesSize = is.read(receiveBuffer);//读取数据长度,InputStream要读取的数据长度一定要小于等于缓冲区中的字节数 103 System.out.println("#######readBytesSize:"+readBytesSize); 104 105 while(readBytesSize != -1){//判断流是否位于文件末尾而没有可用的字节 106 bos.write(receiveBuffer, 0, readBytesSize);//从receiveBuffer内存处的0偏移开始写,写与readBytesSize长度相等的字节 107 readBytesSize = is.read(receiveBuffer); 108 System.out.println("#######bos:"+bos); 109 } 110 reqData = new String(bos.toByteArray(), "UTF-8");//编码后的tr2报文 111 } 112 System.out.println("tr2报文:"+reqData); 113 respXml= ParseUtil.parseXML(reqData);//给解析XML的函数传递快钱返回的TR2的XML数据流 114 115 } 116 catch(Exception e) 117 { 118 System.out.println("发送POST请求出现异常!" + e); 119 e.printStackTrace(); 120 } 121 //使用finally块来关闭输出流、输入流 122 finally 123 { 124 if (out != null){out.close();} 125 //if (in != null){in.close();} 126 } 127 return respXml; 128 } 129 130 }
import了sun开头的类,虽然它在代码里压根就没派上用处!但是必须得引用!
标签:
原文地址:http://www.cnblogs.com/fhtwins/p/4240362.html