一、接口申请开通
本代码是基于天聚人合的话费充值API实现的话费充值功能,使用前需要:
- 通过https://www.tianjurenhe.com/docs/api/?id=2申请开通话费接口服务。
- 你可以在个人中心 ➡️ 数据中心 ➡️ 我的API 模块看到此接口的调用凭证请求key
- 与人合签订相关服务合同后,才能正式使用。前期您也可以申请开通测试环境,进行对接测试。
- 详细的接口说明,可参考聚合官方文档。
二、接口使用
2.1、导入项目需要的包
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics;
using Xfrog.Net;
using System.Security.Cryptography;
2.2、配置话费充值接口地址与一些必须的参数
以下的参数按自己需求配置,如不需调用订单状态查询接口,删除与其相关的参数即可,如ordersta等。在下方主要业务代码注释处粘贴2.3-2.7所介绍的代码
namespace mobile_pay
{
public class Pay
{
#region 话费充值接口地址
//申请的接口Appkey
static string key = "********";
//在个人中心查询
static string openId = "*******";
//自行生成
static string orderid = "*******";
//检测手机号码是否能充值
static string telcheck = "http://op.tianjurenhe.com/ofpay/mobile/telcheck?cardnum=30&phoneno=18888888888&key=" + key;
//根据手机号和面值查询商品信息
static string telquery = "http://op.tianjurenhe.com/ofpay/mobile/telquery?cardnum=30&phoneno=18888888888&key=" + key;
//md5加密
static string sign = md5(openId + key + "18888888888" + "50" + orderid);
//手机直充接口
static string onlineorder = "http://op.tianjurenhe.com/ofpay/mobile/onlineorder?phoneno=18888888888&cardnum=100&orderid=20151010&sign=" + sign + "&key=" + key;
//订单状态查询
static string ordersta = "http://op.tianjurenhe.com/ofpay/mobile/ordersta?orderid=" + orderid + "&key=" + key;
#endregion
//此处加入2.3-2.7的主要业务代码
}
}</param name="result"></param name="s"></param name="postdatastr"></param name="url">
2.3、配置主函数调用接口
以下按自己需求调用http请求,如不需调用订单状态查询接口,删除对应方法即可,如HttpGet(ordersta, “”)等
#region 接口调用
public static void Main(string[] args)
{
//检测手机号码是否能充值
string result = HttpGet(telcheck, "");
string error_code = Json(result);
Debug.WriteLine(result);
Debug.WriteLine(error_code);
//根据手机号和面值查询商品信息
string result1 = HttpGet(telquery, "");
Debug.WriteLine(result1);
//根手机直充接口
string result2 = HttpGet(onlineorder, "");
Debug.WriteLine(result2);
//订单状态查询
string result4 = HttpGet(ordersta, "");
Debug.WriteLine(result4);
}
#endregion
2.4、配置GET方式Http请求方法
#region GET方式Http请求方法
/// <summary>
/// Http请求GET方式
/// </summary>
/// <param name="url">请求地址
/// <param name="postdatastr">请求参数
/// <returns></returns>
public static string HttpGet(string Url, string postDataStr)
{
string retString = "";
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ReadWriteTimeout = 5000;
request.ContentType = "text/html;charset=UTF-8";
request.KeepAlive = false;
request.ServicePoint.ConnectionLimit = 100;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
response.Close();
}
catch (Exception err)
{
//异常消息
Console.WriteLine(err);
System.Diagnostics.Trace.WriteLine(err);
}
//返回响应数据
return retString;
}
#endregion
2.5、配置MD5加密方法
#region MD5加密
/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="s">
/// <returns></returns>
public static String md5(String s)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(s);
bytes = md5.ComputeHash(bytes);
md5.Clear();
string ret = "";
for (int i = 0; i < bytes.Length; i++)
{
ret += Convert.ToString(bytes[i], 16).PadLeft(2, '0');
}
return ret.PadLeft(32, '0');
}
#endregion
2.6、配置json解析方法
示例中引用JsonObject类下载地址:http://download.csdn.net/download/gcm3206021155665/7458439
#region json解析
/// <summary>
/// json解析
/// </summary>
/// <param name="result">
/// <returns></returns>
public static string Json(string result)
{
//创建json对象
JsonObject newObj = new JsonObject(result);
//解析返回数据字段
String error_code = newObj["error_code"].Value;
return error_code;
}
#endregion