- 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;
- //历史订单列表检索
- static string orderlist = "http://op.juhe.cn/ofpay/mobile/orderlist?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);
- //历史订单列表检索
- string result5 = HttpGet(orderlist, "");
- Debug.WriteLine(result5);
- }
- #endregion
- #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
- #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
- #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
- }
- }</param name="result"></param name="s"></param name="postdatastr"></param name="url">
- //该片段来自于http://www.codesnippet.cn/detail/2605201614702.html
来源: http://www.codesnippet.cn/detail/2605201614702.html