18.2.3 使用支付宝实现在线支付(4)
【代码解析】代码中通过调用StringBuilder类,来构造MD5加密算法。
BubbleSort()方法的具体实现代码如下:
- public static string[] BubbleSort(string[] r)
- {
- /// <summary>
- /// 冒泡排序法
- /// </summary>
- int i, j; //交换标志
- string temp;
- bool exchange;
- for (i = 0; i < r.Length; i++) //最多做R.Length-1趟排序
- {
- exchange = false; //本趟排序开始前,交换标志应为假
- for (j = r.Length - 2; j >= i; j--)
- {
- if (System.String.CompareOrdinal(r[j + 1], r[j]) < 0) //交换条件
- {
- //交换数据中的数据
- temp = r[j + 1];
- r[j + 1] = r[j];
- r[j] = temp;
- exchange = true; //发生了交换,故将交换标志置为真
- }
- }
- if (!exchange) //本趟排序未发生交换,提前终止算法
- {
- break;
- }
- }
- return r;
- }
【代码解析】代码中通过使用双重for循环语句,来构造冒泡排序算法。
Get_Http()方法的具体实现代码如下:
- //获取远程服务器ATN结果
- public String Get_Http(String a_strUrl, int timeout)
- {
- string strResult;
- try
- {
- HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create
- (a_strUrl);
- myReq.Timeout = timeout;
- HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
- Stream myStream = HttpWResp.GetResponseStream();
- StreamReader sr = new StreamReader(myStream, Encoding.Default);
- StringBuilder strBuilder = new StringBuilder();
- while (-1 != sr.Peek())
- {
- strBuilder.Append(sr.ReadLine());
- }
- strResult = strBuilder.ToString();
- }
- catch (Exception exp)
- {
- strResult = "错误:" + exp.Message;
- }
- return strResult; //返回结果
- }
【代码解析】代码中通过使用HttpWebRequest对象、myReq对象和HttpWebResponse等对象,来获取远程服务器的ATN结果。
说明:限于篇幅,这里只介绍了实现支付宝接口的主要代码,有关本实例的详细程序代码请参阅光盘中的实例。
联系客服