分享

阿里软件平台HelloWorld开发实例(3)

admin 发表于 2013-10-19 23:05:08 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 0 5904
HelloWorld源代码
我们的HelloWorld程序实现了一个识别和验证阿里巴巴用户,并在页面上输出不同的结果。下面是HelloWorld的源程序。
Default.aspx
﹤%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %﹥

﹤!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"﹥

﹤html xmlns="http://www.w3.org/1999/xhtml"﹥
﹤head runat="server"﹥
    ﹤title﹥阿里旺旺软件平台HelloWorld示例﹤/title﹥
﹤/head﹥
﹤body﹥
    ﹤asp:Label ID="helloText" runat="server"﹥﹤/asp:Label﹥
﹤/body﹥
﹤/html﹥

Default.aspx.cs
using System;
using System.Web;
using System.Security.Cryptography;
using System.Text;
using System.Net;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //准备调用参数:
        string userId = Request.Params["user_id"];
        string appId = Request.Params["app_id"];
        string appInstanceId = Request.Params["app_instance_id"];
        string token = Request.Params["token"];

        string sip_appkey = appId;
        string sip_apiname = "alisoft.validateUser";
        string sip_timestamp = DateTime.Now.ToString();

        string data = "9c4fb3400ed711de80b8836e34b2dea8";   //CERT_CODE(注册软件时获得的)

        //在CERT_CODE之后,按参数名字母顺序将参数名及其值拼接起来,用以对所有参数签名:
        data += "appId" + appId;
        data += "appInstanceId" + appInstanceId;
        data += "sip_apiname" + sip_apiname;
        data += "sip_appkey" + sip_appkey;
        data += "sip_timestamp" + sip_timestamp;
        data += "token" + token;
        data += "userId" + userId;

        //用MD5算法对调用参数进行签名:
        MD5CryptoServiceProvider MD5 = new MD5CryptoServiceProvider();
        string sip_sign = BitConverter.ToString(MD5.ComputeHash(Encoding.UTF8.GetBytes(data)));
        sip_sign = sip_sign.Replace("-", "");   //一定要去除结果中的“-”!

        //拼装调用参数:
        string api_params = "sip_appkey=" + sip_appkey + "&sip_apiname=" + sip_apiname +
"&sip_timestamp=" + sip_timestamp + "&sip_sign=" + sip_sign + "&userId=" + userId +
"&appId=" + appId + "&appInstanceId=" + appInstanceId + "&token=" + token;

        //发出对阿里旺旺软件平台的WEB调用:
        WebRequest request = WebRequest.Create("http://sipdev.alisoft.com/sip/rest?" + api_params);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        WebResponse response = request.GetResponse();

        //根据返回结果进行相应处理
        if (response.Headers.Get("sip_status") == "9999")   //调用平台成功
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(response.GetResponseStream());
            switch (int.Parse(xml.SelectSingleNode("String").InnerText))
            {
                case 0:  say("HelloWorld,欢迎合法使用者!"); break;
                case 1: say(sip_sign + "HelloWorld,欢迎合法订购者!"); break;
                case -1: say("尚未订购或被授权使用本软件!"); break;
                case -2: say("非法使用,拒绝提供软件服务!"); break;
                default: say("调用参数可能出现错误!"); break;
            };
        }
        else
            say("调用阿里旺旺软件平台失败!");
    }

    private void say(string text)
    {
        helloText.Text = text;  //helloText是一个ASP.NET的Label控件
    }
}

本程序是用Visual Studio 2008编写,并在ASP.NET 2.0环境下测试通过。如果你在测试本程序时遇到“服务器提交了协议冲突. Section=ResponseHeader Detail=CR 后面必须是 LF”的错误,请在你的web.config中的 ﹤configuration﹥ 中加入以下内容:
﹤system.net﹥
    ﹤settings﹥
      ﹤httpWebRequest useUnsafeHeaderParsing="true"/﹥
    ﹤/settings﹥
  ﹤/system.net﹥

没找到任何评论,期待你打破沉寂

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条