一、接口申请开通
本代码是基于天聚人合的话费充值API实现的话费充值功能,使用前需要:
- 通过https://www.tianjurenhe.com/docs/api/?id=1申请开通话费接口服务。
– 你可以在个人中心 ➡️ 数据中心 ➡️ 我的API 模块看到此接口的调用凭证请求key - 与人合签订相关服务合同后,才能正式使用。前期您也可以申请开通测试环境,进行对接测试。
- 详细的接口说明,可参考聚合官方文档。
二、接口使用
2.1、引入项目需要的包
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;
2.2、配置appkey与主函数
在下方主要业务的注释处加入2.3-2.7的接口调用代码,在调用到的方法注释处加入2.8的方法代码
namespace ConsoleAPI
{
class Program
{
static void Main(string[] args)
{
string appkey = "*******************"; //配置您申请的appkey
//此处写调用接口的主要业务(2.3-2.4)
}
//此处写业务中调用到的方法(2.5)
}
}
2.3、订单状态查询
string url1 = "http://op.tianjurenhe.com/ofpay/sinopec/ordersta";
var parameters1 = new Dictionary<string, string>();
parameters1.Add("orderid" , ""); //商家订单号,8-32位字母数字组合
parameters1.Add("key", appkey);//你申请的key
string result1 = sendPost(url1, parameters1, "get");
JsonObject newObj1 = new 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.4、加油卡充值
string url3 = "https://www.tianjurenhe.com/docs/api/?id=1";
var parameters3 = new Dictionary<string, string>();
parameters3.Add("cardid" , ""); //产品id:10000(中石化50元加油卡)[暂不支持]、10001(中石化100元加油卡)、10003(中石化500元加油卡)、10004(中石化1000元加油卡)、10007(中石化任意金额充值)[暂不支持]、10008(中石油任意金额充值)
parameters3.Add("cardnum" , ""); //充值数量(产品id为10007、10008时为具体充值金额(整数),其余产品id请传固定值1);注:中石油任意冲(产品id:10008)暂时只支持100\200\500\1000
parameters3.Add("orderid" , ""); //商家订单号,8-32位字母数字组合
parameters3.Add("game_userid" , ""); //加油卡卡号,中石化:以100011开头的卡号、中石油:以9开头的卡号
parameters3.Add("gasCardTel" , ""); //持卡人手机号码,可以填写一个固定格式的手机号码,如:18900000000
parameters3.Add("gasCardName" , ""); //持卡人姓名
parameters3.Add("chargeType" , ""); //加油卡类型 (1:中石化、2:中石油;默认为1)
parameters3.Add("key", appkey);//你申请的key
parameters3.Add("sign" , ""); //校验值,md5(OpenID+key+proid+cardnum+game_userid+orderid),OpenID在个人中心查询
string result3 = sendPost(url3, parameters3, "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);
}
}
2.5、以上代码所调用的方法
/// <summary>
/// Http (GET/POST)
/// </summary>
/// <param name="url">请求URL</param>
/// <param name="parameters">请求参数</param>
/// <param name="method">请求方法</param>
/// <returns>响应内容</returns>
static string sendPost(string url, IDictionary<string, string> parameters, string method)
{
if (method.ToLower() == "post")
{
HttpWebRequest req = null;
HttpWebResponse rsp = null;
System.IO.Stream reqStream = null;
try
{
req = (HttpWebRequest)WebRequest.Create(url);
req.Method = method;
req.KeepAlive = false;
req.ProtocolVersion = 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.GetResponse();
Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
return GetResponseAsString(rsp, encoding);
}
catch (Exception ex)
{
return ex.Message;
}
finally
{
if (reqStream != null) reqStream.Close();
if (rsp != null) rsp.Close();
}
}
else
{
//创建请求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url + "?" + 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 = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
//返回内容
string retString = myStreamReader.ReadToEnd();
return retString;
}
}
/// <summary>
/// 组装普通文本请求参数。
/// </summary>
/// <param name="parameters">Key-Value形式请求参数字典</param>
/// <returns>URL编码后的请求数据</returns>
static string BuildQuery(IDictionary<string, string> parameters, string encode)
{
StringBuilder postData = new StringBuilder();
bool hasParam = false;
IEnumerator<KeyValuePair<string, string>> dem = parameters.GetEnumerator();
while (dem.MoveNext())
{
string name = dem.Current.Key;
string value = dem.Current.Value;
// 忽略参数名或参数值为空的参数
if (!string.IsNullOrEmpty(name))//&& !string.IsNullOrEmpty(value)
{
if (hasParam)
{
postData.Append("&");
}
postData.Append(name);
postData.Append("=");
if (encode == "gb2312")
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.GetEncoding("gb2312")));
}
else if (encode == "utf8")
{
postData.Append(HttpUtility.UrlEncode(value, Encoding.UTF8));
}
else
{
postData.Append(value);
}
hasParam = true;
}
}
return postData.ToString();
}
/// <summary>
/// 把响应流转换为文本。
/// </summary>
/// <param name="rsp">响应流对象</param>
/// <param name="encoding">编码方式</param>
/// <returns>响应文本</returns>
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();
}
}