.net微信公众号开发——基础接口
本文讲述微信公众号开发中基础接口的使用,包括以下内容: (1)获取许可令牌(AccessToken); (2)获取微信服务器地址; (3)上传、下载多媒体文件; (4)创建、显示二维码; (5)长链接转短链接。 开源项目地址:http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount 本文的源代码地址:http://git.oschina.net/xrwang2/xrwang.weixin.PublicAccount/blob/master/xrwang.net/Example/BasicInterface.aspx.cs 本文的演示地址:http://xrwang.net/Example/BasicInterface.aspx 演示效果如下图: 1 获取许可令牌 /// <summary> /// 获取许可令牌 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGetAccessToken_Click(object sender,EventArgs e) { string userName = lbPublicAccount.SelectedValue; AccessToken token = AccessToken.Get(userName); txtAccessToken.Text = token != null ? token.access_token : "获取许可令牌失败。"; } 2 获取微信服务器地址 /// <summary> /// 获取微信服务器地址 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGetServerAddress_Click(object sender, EventArgs e) { ErrorMessage errorMessage; ServerAddresses addresses = ServerAddresses.Get(out errorMessage); if (errorMessage.IsSuccess && addresses.ip_list != null) { StringBuilder sb = new StringBuilder(); foreach (string ip in addresses.ip_list) sb.AppendFormat("{0},", ip); txtServerAddress.Text = sb.ToString(); } else txtServerAddress.Text = string.Format("获取微信服务器地址失败。{0}", errorMessage); } 3 上传、下载多媒体文件 /// <summary> /// 上传多媒体文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnUpload_Click(object sender, EventArgs e) { string userName = lbPublicAccount.SelectedValue; MultiMediaTypeEnum type = (MultiMediaTypeEnum)Enum.Parse(typeof(MultiMediaTypeEnum), lbMultiMediaType.SelectedValue); string filename = fileUpload.FileName; byte[] bytes = fileUpload.FileBytes; ErrorMessage errorMessage; MultiMediaUploadResult result = MultiMediaHelper.Upload(userName, type, filename, bytes, out errorMessage); if (errorMessage.IsSuccess && result != null) hlShowMultiMedia.NavigateUrl = MultiMediaHelper.GetDownloadUrl(AccessToken.Get(userName).access_token, result.MediaId); else hlShowMultiMedia.NavigateUrl = string.Format("javascript:alert('上传多媒体文件失败。/r/n{0}');", errorMessage); }上传多媒体文件,并生成下载链接的示例 4 创建、显示二维码 /// <summary> /// 创建二维码 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnCreateQrCode_Click(object sender, EventArgs e) { string userName = lbPublicAccount.SelectedValue; string strSceneId = txtSceneId.Text; QrCode qrcode=null; ErrorMessage errorMessage; if( cbIsTemple.Checked) { int expireSeconds = int.Parse(txtExpireSeconds.Text); int sceneId; if (int.TryParse(strSceneId, out sceneId)) qrcode = QrCode.Create(userName, expireSeconds, sceneId, out errorMessage); else errorMessage = new ErrorMessage(ErrorMessage.ExceptionCode, "场景值id必须为整数。"); } else { int sceneId; if (int.TryParse(strSceneId, out sceneId)) qrcode = QrCode.Create(userName, sceneId, out errorMessage); else qrcode = QrCode.Create(userName, strSceneId, out errorMessage); } if (errorMessage.IsSuccess && qrcode != null) imgQrCode.ImageUrl = QrCode.GetUrl(qrcode.ticket); else imgQrCode.ImageUrl = ""; } 5 长链接转短链接 ShortUrl封装了长连接转短链接的方法。 /// <summary> /// 获取短链接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnGetShortUrl_Click(object sender, EventArgs e) { string userName = lbPublicAccount.SelectedValue; ErrorMessage errorMessage; string shortUrl = ShortUrl.Get(userName, txtLongUrl.Text, out errorMessage); if (errorMessage.IsSuccess && string.IsNullOrWhiteSpace(shortUrl)) txtShortUrl.Text = shortUrl; else txtShortUrl.Text = string.Format("获取短链接失败。{0}", errorMessage); } 感谢您看完本文,希望对您有所帮助。 更多.net微信公众号开发——基础接口相关文章请关注PHP中文网! |
- 上一篇:微信公众平台开发 上传下载多媒体文件
- 下一篇:微信公众平台开发:高级接口说明