1.示例代码中调用接口地址:http://www.juhe.cn/docs/api/id/85/aid/212
2.示例中引用JsonObject类下载地址:http://download.csdn.net/download/gcm3206021155665/7458439
具体代码如下:
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; namespace mobile_pay { public class Pay { #region 话费充值接口地址 //申请的接口Appkey static string key = "********"; //在个人中心查询 static string openId = "*******"; //自行生成 static string orderid = "*******"; //检测手机号码是否能充值 static string telcheck = "http://op.juhe.cn/ofpay/mobile/telcheck?cardnum=30&phoneno=18888888888&key=" + key; //根据手机号和面值查询商品信息 static string telquery = "http://op.juhe.cn/ofpay/mobile/telquery?cardnum=30&phoneno=18888888888&key=" + key; //md5加密 static string sign = md5(openId + key + "18888888888" + "50" + orderid); //手机直充接口 static string onlineorder = "http://op.juhe.cn/ofpay/mobile/onlineorder?phoneno=18888888888&cardnum=100&orderid=20151010&sign=" + sign + "&key=" + key; //时间截 static string timestamp = DateTime.Now.ToString(); //md5加密 static string sign1 = md5(openId + key + timestamp); //账户余额查询 static string yue = "http://op.juhe.cn/ofpay/mobile/yue?timestamp=" + timestamp + "&sign=" + sign1 + "&key=" + key; //订单状态查询 static string ordersta = "http://op.juhe.cn/ofpay/mobile/ordersta?orderid=" + orderid + "&key=" + key; #endregion #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 result3 = HttpGet(yue, ""); Debug.WriteLine(result3); //订单状态查询 string result4 = HttpGet(ordersta, ""); Debug.WriteLine(result4); } #endregion #region GET方式Http请求方法 ////// Http请求GET方式 /// /// 请求地址 /// 请求参数 ///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 #region MD5加密 /// /// 32位MD5加密 /// /// ///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 #region json解析 /// /// json解析 /// /// ///public static string Json(string result) { //创建json对象 JsonObject newObj = new JsonObject(result); //解析返回数据字段 String error_code = newObj["error_code"].Value; return error_code; } #endregion } }