.net如何获取接口返回值
发布网友
发布时间:2022-04-25 19:53
我来回答
共4个回答
热心网友
时间:2022-04-28 01:19
参考实例如下:
调用方法:
1、引入CommonAliPay.dll
2、实现接口的方法调用方式:
AliPay ap = new AliPay();
string key = "";//填写自己的key
string partner = "";//填写自己的Partner
StandardGoods bp = new StandardGoods("trade_create_by_buyer", partner, key, "MD5", "卡2", Guid.NewGuid().ToString(), 2.551m, 1, "hao_ding2000@yahoo.com.cn", "hao_ding2000@yahoo.com.cn"
, "EMS", 25.00m, "BUYER_PAY","1");
bp.Notify_Url = "http://203.86.79.185/ali/notify.aspx";
ap.CreateStandardTrade("https://www.alipay.com/cooperate/gateway.do", bp, this);上面是通用的调用方式。
下面是只支持虚拟货物的方式:
string key = "";//填写自己的key
string partner = "";//填写自己的Partner
AliPay ap = new AliPay();
DigitalGoods bp = new DigitalGoods("create_digital_goods_trade_p", partner, key, "MD5", "卡2", Guid.NewGuid().ToString(), 2.551m, 1, "hao_ding2000@yahoo.com.cn", "hao_ding2000@yahoo.com.cn");
bp.Notify_Url = "http://203.86.79.185/ali/notify.aspx";
ap.CreateDigitalTrade("https://www.alipay.com/cooperate/gateway.do", bp, this);3、实现通知接口方法的调用(支持虚拟和实物):
protected void Page_Load(object sender, EventArgs e)
{
string key = "";//填写自己的key
string partner = "";//填写自己的Partner
AliPay ap = new AliPay();
string notifyid = Request.Form["notify_id"];
Verify v = new Verify("notify_verify", partner, notifyid);
ap.WaitSellerSendGoods+=new AliPay.ProcessNotifyEventHandler(ap_WaitSellerSendGoods);
ap.WaitBuyerPay += new AliPay.ProcessNotifyEventHandler(ap_WaitBuyerPay);
ap.ProcessNotify(this, "https://www.alipay.com/cooperate/gateway.do",key,v, "utf-8");
}
void ap_WaitBuyerPay(object sender, NotifyEventArgs e)
{
// //加入自己的处理逻辑
Log4net.log.Error("wait buyer pay fire");
}
private void ap_WaitSellerSendGoods(object sender, NotifyEventArgs e)
{
//加入自己的处理逻辑
Log4net.log.Error("WaitSellerSendGoods fire");
}的交易状态都被定义成了类似名称的事件。
部分源代码解析:
1、解析Forms集合到NotifyEventArgs类,因为后面此类的数据要用来做MD5Sign,所以所有值类型,不能存在初始值,如:int的0等。因此用Nullable范型。
private NotifyEventArgs ParseNotify(NameValueCollection nv, object obj)
{
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo pi in propertyInfos)
{
string v = nv.Get(pi.Name.ToLower());
if (v != null)
{
if (pi.PropertyType == typeof(string))
{
pi.SetValue(obj, v, null);
}
else if (pi.PropertyType == typeof(int?))
{
pi.SetValue(obj, int.Parse(v), null);
}
else if (pi.PropertyType == typeof(decimal?))
{
pi.SetValue(obj, decimal.Parse(v), null);
}
else if (pi.PropertyType == typeof(DateTime?))
{
pi.SetValue(obj, DateTime.Parse(v), null);
}
else if (pi.PropertyType == typeof(bool))
{
pi.SetValue(obj, bool.Parse(v), null);
}
else
{
//转型失败会抛出异常
pi.SetValue(obj, v, null);
}
}
}
return (NotifyEventArgs)obj;
}
2、从类型中获取排序后的参数
/**//// <summary>
/// 获取排序后的参数
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private SortedList<string,string> GetParam(object obj)
{
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(BindingFlags.Public|BindingFlags.Instance);
SortedList<string, string> sortedList = new SortedList<string, string>(StringComparer.CurrentCultureIgnoreCase);
foreach (PropertyInfo pi in propertyInfos)
{
if (pi.GetValue(obj, null) != null)
{
if (pi.Name == "Sign" || pi.Name == "Sign_Type")
{
continue;
}
sortedList.Add(pi.Name.ToLower(), pi.GetValue(obj, null).ToString());
}
}
return sortedList;
}3、从SortedList中产生参数
private string GetUrlParam(SortedList<string, string> sortedList,bool isEncode)
{
StringBuilder param = new StringBuilder();
StringBuilder encodeParam = new StringBuilder();
if (isEncode == false)
{
foreach (KeyValuePair<string, string> kvp in sortedList)
{
string t = string.Format("{0}={1}", kvp.Key, kvp.Value);
param.Append(t + "&");
}
return param.ToString().TrimEnd('&');
}
else
{
foreach (KeyValuePair<string, string> kvp in sortedList)
{
string et = string.Format("{0}={1}", HttpUtility.UrlEncode(kvp.Key), HttpUtility.UrlEncode(kvp.Value));
encodeParam.Append(et + "&");
}
return encodeParam.ToString().TrimEnd('&');
}
热心网友
时间:2022-04-28 02:37
你的代码里不都是会写吗?
doc.SelectSingleNode("response/result/code").InnerText得到的结果就是0
doc.SelectSingleNode("response/data/id").InnerText得到的结果就是你要的178这个id
热心网友
时间:2022-04-28 04:12
XmlDocument把XML文件load进来,解析节点。追问引用 1 楼 的回复:
用XML类 序列化
大大 可以说得详细些吗?万份感谢,在线等,急呀
热心网友
时间:2022-04-28 06:03
使用XML进行解析即可