1、基于 C#的加油卡接口调用代码实例代码描述:基于 C#的加油卡接口调用代码实例代码平台:聚合数据using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net;using System.IO;using Xfrog.Net;using System.Diagnostics;using System.Web;/-/ 加油卡充值调用示例代码 聚合数据/ 在线接口文档:http:/ 代码中 JsonObject类下载地址:http:/ ConsoleAPIclass
2、Programstatic void Main(string args)string appkey = “*“; /配置您申请的 appkey/1.订单状态查询string url1 = “http:/ parameters1 = new Dictionary();parameters1.Add(“orderid“ , “); /商家订单号,8-32 位字母数字组合parameters1.Add(“key“, appkey);/你申请的 keystring result1 = sendPost(url1, parameters1, “get“);JsonObject newObj1 = new
3、 JsonObject(result1);String errorCode1 = newObj1“error_code“.Value;if (errorCode1 = “0“)Debug.WriteLine(“成功“ );Debug.WriteLine(newObj1);else/Debug.WriteLine(“失败“);Debug.WriteLine(newObj1“error_code“.Value+“:“+newObj1“reason“.Value);/2.账户余额查询string url2 = “http:/ parameters2 = new Dictionary();parame
4、ters2.Add(“timestamp“ , “); /当前时间戳,如:1432788379parameters2.Add(“key“, appkey);/你申请的 keyparameters2.Add(“sign“ , “); /校验值,md5(OpenID+key+timestamp), OpenID 在个人中心 查询string result2 = sendPost(url2, parameters2, “get“);JsonObject newObj2 = new JsonObject(result2);String errorCode2 = newObj2“error_code“.
5、Value;if (errorCode2 = “0“)Debug.WriteLine(“成功“ );Debug.WriteLine(newObj2);else/Debug.WriteLine(“失败“);Debug.WriteLine(newObj2“error_code“.Value+“:“+newObj2“reason“.Value);/3.加油卡充值string url3 = “http:/ parameters3 = new Dictionary();parameters3.Add(“cardid“ , “); /产品 id:10000(中石化 50 元加油卡) 、 10001(中石化
6、 100 元加油卡 )、10003(中石化 500 元加油卡)、10004(中石化 1000 元加油卡 )、10007(中石化任意金额充值)、10008(中石油任意金 额充值)parameters3.Add(“cardnum“ , “); /充值数量 任意充 (整数(元),其余面值固定值为 1parameters3.Add(“orderid“ , “); /商家订单号,8-32 位字母数字组合parameters3.Add(“game_userid“ , “); /加油卡卡号,中石化:以100011 开头的卡号、中石油:以 9 开头的卡号parameters3.Add(“gasCardTel“
7、 , “); /持卡人手机号码parameters3.Add(“gasCardName“ , “); /持卡人姓名parameters3.Add(“chargeType“ , “); /加油卡类型 (1:中石化、2:中石油;默认为 1)parameters3.Add(“key“, appkey);/你申请的 keyparameters3.Add(“sign“ , “); /校验值,md5(OpenID+key+proid+cardnum+game_userid+orderid),OpenID 在个人中心 查询string result3 = sendPost(url3, parameters3
8、, “get“);JsonObject newObj3 = new JsonObject(result3);String errorCode3 = newObj3“error_code“.Value;if (errorCode3 = “0“)Debug.WriteLine(“成功“ );Debug.WriteLine(newObj3);else/Debug.WriteLine(“失败“);Debug.WriteLine(newObj3“error_code“.Value+“:“+newObj3“reason“.Value);/ / Http (GET/POST)/ / 请求 URL/ 请求参数
9、/ 请求方法/ 响应内容static string sendPost(string url, IDictionary parameters, string method)if (method.ToLower() = “post“)HttpWebRequest req = null;HttpWebResponse rsp = null;System.IO.Stream reqStream = null;tryreq = (HttpWebRequest)WebRequest.Create(url);req.Method = method;req.KeepAlive = false;req.Prot
10、ocolVersion = HttpVersion.Version10;req.Timeout = 5000;req.ContentType = “application/x-www-form-urlencoded;charset=utf-8“;byte postData = Encoding.UTF8.GetBytes(BuildQuery(parameters, “utf8“);reqStream = req.GetRequestStream();reqStream.Write(postData, 0, postData.Length);rsp = (HttpWebResponse)req
11、.GetResponse();Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);return GetResponseAsString(rsp, encoding);catch (Exception ex)return ex.Message;finallyif (reqStream != null) reqStream.Close();if (rsp != null) rsp.Close();else/创建请求HttpWebRequest request = (HttpWebRequest)WebRequest.Create(u
12、rl + “?“ + BuildQuery(parameters, “utf8“);/GET请求request.Method = “GET“;request.ReadWriteTimeout = 5000;request.ContentType = “text/html;charset=UTF-8“;HttpWebResponse response = (HttpWebResponse)request.GetResponse();Stream myResponseStream = response.GetResponseStream();StreamReader myStreamReader
13、= new StreamReader(myResponseStream, Encoding.GetEncoding(“utf-8“);/返回内容string retString = myStreamReader.ReadToEnd();return retString;/ / 组装普通文本请求参数。/ / Key-Value 形式请求参数字典/ URL编码后的请求数据static string BuildQuery(IDictionary parameters,string encode)StringBuilder postData = new StringBuilder();bool has
14、Param = false;IEnumerator dem = parameters.GetEnumerator();while (dem.MoveNext()string name = dem.Current.Key;string value = dem.Current.Value;/ 忽略参数名或参数值为空的参数if (!string.IsNullOrEmpty(name)/postData.Append(name);postData.Append(“=“);if (encode = “gb2312“)postData.Append(HttpUtility.UrlEncode(value,
15、 Encoding.GetEncoding(“gb2312“);else if (encode = “utf8“)postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8);elsepostData.Append(value);hasParam = true;return postData.ToString();/ / 把响应流转换为文本。/ / 响应流对象/ 编码方式/ 响应文本static string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)System.IO.Stream stream = null;StreamReader reader = null;try/ 以字符流的方式读取 HTTP 响应stream = rsp.GetResponseStream();reader = new StreamReader(stream, encoding);return reader.ReadToEnd();finally/ 释放资源if (reader != null) reader.Close();if (stream != null) stream.Close();if (rsp != null) rsp.Close();