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://www.juhe.cn/docs/85
// 代码中JsonObject类下载地址:http://download.csdn.net/download/gcm3206021155665/7458439
//----------------------------------
namespace ConsoleAPI
{
class Program
{
static void Main(string[] args)
{
string appkey = "*******************"; //配置您申请的appkey
//1.账户余额查询
string url1 = "http://op.juhe.cn/ofpay/mobile/yue";
var parameters1 = new Dictionary();
parameters1.Add("timestamp" , ""); //当前时间戳,如:1432788379
parameters1.Add("key", appkey);//你申请的key
parameters1.Add("sign" , ""); //校验值,md5(OpenID+key+timestamp),OpenID在个人中心查询
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.订单状态查询
string url2 = "http://op.juhe.cn/ofpay/mobile/ordersta";
var parameters2 = new Dictionary();
parameters2.Add("orderid" , ""); //商家订单号,8-32位字母数字组合
parameters2.Add("key", appkey);//你申请的key
string result2 = sendPost(url2, parameters2, "get");
JsonObject newObj2 = new JsonObject(result2);
String errorCode2 = newObj2["error_code"].Value;
if (errorCode2 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj2);
}
else
{
//Debug.WriteLine("失败");
Debug.WriteLine(newObj2["error_code"].Value+":"+newObj2["reason"].Value);
}
//5.检测手机号码是否能充值
string url5 = "http://op.juhe.cn/ofpay/mobile/telcheck";
var parameters5 = new Dictionary();
parameters5.Add("phoneno" , ""); //手机号码
parameters5.Add("cardnum" , ""); //充值金额,目前可选:5、10、20、30、50、100、300
parameters5.Add("key", appkey);//你申请的key
string result5 = sendPost(url5, parameters5, "get");
JsonObject newObj5 = new JsonObject(result5);
String errorCode5 = newObj5["error_code"].Value;
if (errorCode5 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj5);
}
else
{
//Debug.WriteLine("失败");
Debug.WriteLine(newObj5["error_code"].Value+":"+newObj5["reason"].Value);
}
//6.根据手机号和面值查询商品信息
string url6 = "http://op.juhe.cn/ofpay/mobile/telquery";
var parameters6 = new Dictionary();
parameters6.Add("phoneno" , ""); //手机号码
parameters6.Add("cardnum" , ""); //充值金额,目前可选:5、10、20、30、50、100、300
parameters6.Add("key", appkey);//你申请的key
string result6 = sendPost(url6, parameters6, "get");
JsonObject newObj6 = new JsonObject(result6);
String errorCode6 = newObj6["error_code"].Value;
if (errorCode6 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj6);
}
else
{
//Debug.WriteLine("失败");
Debug.WriteLine(newObj6["error_code"].Value+":"+newObj6["reason"].Value);
}
//7.手机直充接口
string url7 = "http://op.juhe.cn/ofpay/mobile/onlineorder";
var parameters7 = new Dictionary();
parameters7.Add("phoneno" , ""); //手机号码
parameters7.Add("cardnum" , ""); //充值金额,目前可选:5、10、20、30、50、100、300
parameters7.Add("orderid" , ""); //商家订单号,8-32位字母数字组合
parameters7.Add("key", appkey);//你申请的key
parameters7.Add("sign" , ""); //校验值,md5(OpenID+key+phoneno+cardnum+orderid),OpenID在个人中心查询
string result7 = sendPost(url7, parameters7, "get");
JsonObject newObj7 = new JsonObject(result7);
String errorCode7 = newObj7["error_code"].Value;
if (errorCode7 == "0")
{
Debug.WriteLine("成功");
Debug.WriteLine(newObj7);
}
else
{
//Debug.WriteLine("失败");
Debug.WriteLine(newObj7["error_code"].Value+":"+newObj7["reason"].Value);
}
}
///
/// Http (GET/POST)
///
/// 请求URL
/// 请求参数
/// 请求方法
/// 响应内容
static string sendPost(string url, IDictionary 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;
}
}
///
/// 组装普通文本请求参数。
///
/// Key-Value形式请求参数字典
/// URL编码后的请求数据
static string BuildQuery(IDictionary parameters, string encode)
{
StringBuilder postData = new StringBuilder();
bool hasParam = false;
IEnumerator> 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();
}
///
/// 把响应流转换为文本。
///
/// 响应流对象
/// 编码方式
/// 响应文本
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();
}
}
}
}
登录 后可发表评论