web api HttpResponseMessage的简单使用
using Lemon.Common;
using Lemon.WeChat.Model;
using Lemon.WeChat.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Senparc.Weixin.MP;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.Http;
using System.Xml; namespace Lemon.WeChat.WebApi
{
/// <summary>
/// 微信回调
/// </summary>
[RoutePrefix("wechat/WeCallback")]
public class WeCallbackController : ApiController
{
private readonly string Token = "lemon95";
/// <summary>
/// 构造函数
/// </summary>
public WeCallbackController()
{
Token = WebConfigurationManager.AppSettings["WeixinToken"] ?? Token;
}
/// <summary>
///
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
/// <param name="echostr"></param>
[Route("")]
public HttpResponseMessage Get(string signature, string timestamp, string nonce, string echostr)
{
Logger.Info(string.Format("参数:signature:{0},timestamp:{1},nonce:{2},echostr:{3}", signature, timestamp, nonce, echostr));
string returnstr = string.Empty;
if (CheckSignature.Check(signature, timestamp, nonce, Token))
{
returnstr = echostr;
}
else
{
returnstr = "验证失败";
} HttpResponseMessage result = new HttpResponseMessage
{
Content = new StringContent(returnstr, Encoding.UTF8, "application/x-www-form-urlencoded")
};
return result;
} /// <summary>
///
/// </summary>
/// <param name="signature"></param>
/// <param name="timestamp"></param>
/// <param name="nonce"></param>
[Route("")]
public HttpResponseMessage Post(string signature, string timestamp, string nonce)
{
Logger.Info(string.Format("参数:signature:{0},timestamp:{1},nonce:{2}", signature, timestamp, nonce)); string returnstr = string.Empty;
try
{
if (!CheckSignature.Check(signature, timestamp, nonce, Token))
{
returnstr = "验证失败";
}
else
{
var requestContent = Request.Content.ReadAsStreamAsync().Result;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(requestContent);
Logger.Info(xmlDoc.InnerXml);
WeCalbackService svr = new WeCalbackService();
returnstr = svr.Callback(xmlDoc);
}
}
catch (Exception ex)
{
Logger.Error("WeCallbackController[Post]发生异常。", ex);
} HttpResponseMessage result = new HttpResponseMessage
{
Content = new StringContent(returnstr, Encoding.UTF8, "application/x-www-form-urlencoded")
};
return result;
} /// <summary>
/// 回调通知
/// </summary>
/// <returns></returns>
[HttpPost, Route("Notify")]
public HttpResponseMessage Notify()
{
string returnstr = string.Empty;
try
{
var requestContent = Request.Content.ReadAsStreamAsync().Result;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(requestContent);
WeCalbackService svr = new WeCalbackService();
returnstr = svr.Notify(xmlDoc);
}
catch (Exception ex)
{
Logger.Error("WeCallbackController[Notify]发生异常。", ex);
}
HttpResponseMessage result = new HttpResponseMessage
{
Content = new StringContent(returnstr, Encoding.UTF8, "application/x-www-form-urlencoded")
};
return result; } }
}
web api HttpResponseMessage的简单使用的更多相关文章
- [angularjs] MVC + Web API + AngularJs 搭建简单的 CURD 框架
MVC + Web API + AngularJs 搭建简单的 CURD 框架 GitHub 地址:https://github.com/liqingwen2015/Wen.MvcSinglePage ...
- ASP.NET - Web API,从简单类型到复杂类型的参数传递用例,以及传递简单string类型的解决办法
一,简单类型的传值 比如 public Users Get(int id) ,它可以使用两种方式获取: api/default/ $.get("/api/default",{id: ...
- 通过Knockout.js + ASP.NET Web API构建一个简单的CRUD应用
REFERENCE FROM : http://www.cnblogs.com/artech/archive/2012/07/04/Knockout-web-api.html 较之面向最终消费者的网站 ...
- web api Route属性定义
ASP.NET Web API路由,简单来说,就是把客户端请求映射到对应的Action上的过程.在"ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置"一 ...
- ASP.NET Web API - ASP.NET MVC 4 系列
Web API 项目是 Windows 通信接口(Windows Communication Foundation,WCF)团队及其用户激情下的产物,他们想与 HTTP 深度整合.WCF ...
- ASP.NET Web API实践系列04,通过Route等特性设置路由
ASP.NET Web API路由,简单来说,就是把客户端请求映射到对应的Action上的过程.在"ASP.NET Web API实践系列03,路由模版, 路由惯例, 路由设置"一 ...
- ASP.NET Web API——选择Web API还是WCF
WCF是.NET平台服务开发的一站式框架,那么为什么还要有ASP.NET Web API呢?简单来说,ASP.NET Web API的设计和构建只考虑了一件事情,那就是HTTP,而WCF的设计主要是考 ...
- Global Error Handling in ASP.NET Web API 2(webapi2 中的全局异常处理)
目前,在Web API中没有简单的方法来记录或处理全局异常(webapi1中).一些未处理的异常可以通过exception filters进行处理,但是有许多情况exception filters无法 ...
- 我所理解的RESTful Web API [设计篇]
<我所理解的RESTful Web API [Web标准篇]>Web服务已经成为了异质系统之间的互联与集成的主要手段,在过去一段不短的时间里,Web服务几乎清一水地采用SOAP来构建.构建 ...
随机推荐
- Java复习总结——详细理解Java反射机制
反射是什么 反射的作用用一句简单的话来讲就是可以对代码进行操作的代码,这个特性经常在被用于创建JavaBean中,通常造轮子的人会用到这个特性,而应用程序员用到这个特性的场景则较少. 能够分析类能力的 ...
- FastDFS 文件上传工具类
FastDFS文件上传工具类 import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; imp ...
- A1143. Lowest Common Ancestor
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 团体程序设计天梯赛(CCCC) L3014 周游世界 BFS证明
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code
- SpringBoot+Shiro+Redis共享Session入门小栗子
在单机版的Springboot+Shiro的基础上,这次实现共享Session. 这里没有自己写RedisManager.SessionDAO.用的 crazycake 写的开源插件 pom.xml ...
- matlab : Nelder mead simplex 单纯形直接搜索算法;
function [ param ] = NeldSearch( param ) %NERDSEARCH 此处显示有关此函数的摘要 % nelder mead simplex 单纯形直接搜索算法: % ...
- 【 强大的Mac/iOS开发工具】AppCode for Mac 2017.3
[简介] 最新的 AppCode 2017.3 版本,完全支持最新的Swift 4.0语言,这是一款JetBrain出品的强大的OS X 和 iOS开发工具,AppCode可以用于开发 Mac OS ...
- codeforces794D dfs+图上hash
http://codeforces.com/problemset/problem/794/D 题意:在一个国家有 n 座城市和一些双向边.这些城市被编号为 1 到 n. 一共有 m 条双线边,第 i条 ...
- Go GraphQL初学者教程
Go GraphQL初学者教程 https://tutorialedge.net/golang/go-graphql-beginners-tutorial/ https://tutorialedge. ...
- linux报错汇总
一.出现cannot send message: Process exited with a non-zero status错误 查看log文件:sudo cat /var/log/mail.err, ...