一般处理程序获取get,post数据
ashx具体代码:
public class GuanWangWC : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
try
{
string method = context.Request.Params["method"];
//post
if (method == "post")
{
this.PostBack(context);
return;
}
if (method == "get")
{
this.GetBack(context);
return;
}
string strip = RequestUtils.GetIp(context);
string msg = "访问IP:" + strip;
ShowResult(context, "", msg);
}
catch (Exception ex)
{
LogUtils.Error("ashx出错=" + ex.Message);
ShowResult(context, "", ex.Message);
}
} private void GetBack(HttpContext context)
{ string strReq = HttpUtility.UrlDecode(context.Request.QueryString.ToString());
LogUtils.Info("请求参数:" + strReq ); SqlConnection conn = new SqlConnection("连接字符串");
try
{
ShowResult(context, "", "ok");
return;
}
catch (Exception ex)
{
ShowResult(context, "", "请求:" + strReq + "==错误信息:" + ex.Message);
LogUtils.Error("请求参数:" + strReq + " 错误信息:" + ex.Message);
}
finally
{
conn.Close();
}
} private void PostBack(HttpContext context)
{ string strReq = new httppost().GetData(context.Request);
LogUtils.Info(string.Format("请求参数:{0}====", strReq));
TicketingRequset shr = SerializeUtil.DeserializeJson<TicketingRequset>(strReq);
if (shr == null)
{
ShowResult(context, "", "数据不能为空");
return;
} SqlConnection conn = new SqlConnection("连接字符串");
try
{
ShowResult(context, "", "ok");
return;
}
catch (Exception ex)
{
ShowResult(context, "", "请求:" + strReq + "==错误信息:" + ex.Message);
LogUtils.Error("====请求参数:" + strReq + " 错误信息:" + ex.Message);
}
finally
{
conn.Close();
}
} /// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="errorCode">0:成功 其他失败</param>
/// <param name="errorMsg"></param>
private void ShowResult(HttpContext context, string errorCode, string errorMsg)
{
string result = string.Format("{{\"errorCode\":{0},\"errorMsg\":\"{1}\"}}", errorCode, errorMsg);
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
}
ashx通用返回model,具体代码:
public class OrderData : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
string type = context.Request.Params["type"];
if (string.IsNullOrEmpty(type))
{
Msg(context, new RspModel<object>(, "type null"));
return;
} try
{
if (type == "AdjWriteLine")
{
AdjWriteLineMethod(context); return;
}
}
catch (Exception ex)
{
Msg(context, new RspModel<object>(, ex.Message));
Common.logclass.Error("出错:" + ex.Message, ex);
return;
} } private void AdjWriteLineMethod(HttpContext context)
{
string strreq = httputil.GetData(context.Request);
// arr = context.Request.Params["arr"],
var req = SerializeUtil.DeserializeJson<AdjWriteLineReq>(strreq);
if (req == null)
{
logclass.Info("AdjWriteLineMethod请求:" + strreq);
Msg(context, new RspModel<AdjWriteLineReq>(, "序列化数据为空(请以json字符post提交)"));
return;
} SqlConnection conn = new SqlConnection(ConfigSetting.SqlConnection);
try
{ RspModel<AdjWriteLineReq> rsp = new RspModel<AdjWriteLineReq>(, "");
rsp.content = _model;//对应泛型 T
Msg(context, rsp);
return; }
catch (Exception ex)
{
logclass.Info("AdjWriteLineMethod请求:" + strreq);
logclass.Error("错误:==" + ex.Message);
Msg(context, new RspModel<AdjWriteLineReq>(, "错误:==" + ex.Message));
return;
}
finally
{
if (conn != null)
conn.Dispose();
} }
private void Msg<T>(HttpContext context, T model)
{
string result = SerializeUtil.SerializeJson(model);
context.Response.ContentType = "text/plain";
context.Response.Write(result);
} #region Model
public class RspModel<T>
{
/// <summary>
/// 0:成功 其他失败
/// </summary>
public int code { get; set; }
public string msg { get; set; }
public T content { get; set; }
public RspModel()
{
code = ;
}
public RspModel(int _code, string _msg)
{
code = _code;
msg = _msg;
}
} #endregion public bool IsReusable
{
get
{
return false;
}
}
}
一般处理程序获取get,post数据的更多相关文章
- jQuery获取一般处理程序(ashx)的JSON数据
昨天有在开发的软件生产线生产流程,RFID扫描IC卡的数据,当中有用到jQuery获取一般处理程序(ashx)的JSON数据.今有把它写成一个小例子,望需要的网友能参考. 在网站中,创建一个一般应用程 ...
- 用struts2标签如何从数据库获取数据并在查询页面显示。最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变量。
最近做一个小项目,需要用到struts2标签从数据库查询数据,并且用迭代器iterator标签在查询页面显示,可是一开始,怎么也获取不到数据,想了许久,最后发现,是自己少定义了一个变量,也就是var变 ...
- 读取TDrawGrid之获取博易数据
朋友叫我帮忙写个从博易读取数据的工具,可无奈数据所在控件并不是Windows标准控件,也就是说没办法通过发送系统消息来获取 相关数据,于是乎试了一下从内存直接读取,可最后并不能达到预期目的,原因是笔者 ...
- C#中,使用正式表达式匹配获取所需数据
.NET中,使用正式表达式匹配获取所需数据 需求:获取一串字符串中,正则匹配出需要的数据. 例如以下字符串: string temp ="ErrorCode:-1,Message:{&quo ...
- 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换
[源码下载] 速战速决 (6) - PHP: 获取 http 请求数据, 获取 get 数据 和 post 数据, json 字符串与对象之间的相互转换 作者:webabcd 介绍速战速决 之 PHP ...
- iOS 直播-获取音频(视频)数据
iOS 直播-获取音频(视频)数据 // // ViewController.m // capture-test // // Created by caoxu on 16/6/3. // Copyri ...
- OpenGL ES无法获取贴图数据原因
最近在做一个项目,要从贴图中获取图像数据,查了很多资料,也琢磨很久,获取到的数据都是0.终于在一次偶然的机会,发现了端倪,成功了. 不得不说这"一分灵感"真的很重要 以下是在获取贴 ...
- 使用腾讯开发平台获取QQ用户数据资料
<今天是七夕:祝大家七夕嗨皮,前可么么哒,后可啪啪啪> Tips:本篇博客将教你如何使用腾讯开发平台获取QQ用户资料 ----------------------------------- ...
- qt qml ajax 获取 json 天气数据示例
依赖ajax.js类库,以下代码很简单的实现了获取天气json数据并展示的任务 [TestAjax.qml] import QtQuick 2.0 import "ajax.js" ...
- HR开发 获取信息类型数据
1.PNP逻辑数据库. LOOP获取信息类型数据. TABLES: PERNR . , . START-OF-SELECTION. GET PERNR . LOOP AT P0000 WHERE .. ...
随机推荐
- IDEAL 热更新
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- redis cluster最简配置
redis cluster最简配置 master配置如下:(默认6379端口) bind 127.0.0.1 port 6379 timeout 0 databases 16 Master的redis ...
- 使用__slots__ __str__ __iter__
__slots__ 为了达到限制的目的,Python允许在定义class的时候,定义一个特殊的__slots__变量,来限制该class实例能添加的属性. __str__ 用这个命令定义方法,可以返 ...
- 【Python】关于decode和encode
#-*-coding:utf-8 import sys ''' *首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码, 即先将 ...
- [转]CentOS7利用systemctl添加自定义系统服务
原文:https://www.cnblogs.com/saneri/p/7778756.html CentOS7自定义系统服务 CentOS7的服务systemctl脚本存放在:/usr/lib/sy ...
- linux怎么实时查看目录下是否有文件生成
inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件. inotify-tools是用 ...
- cocos2dx-lua中handler解析
先看一段代码: local c=c or {} function c:onTouch() print "test in onTouch" end function handler( ...
- Codeforces 1105B:Zuhair and Strings(字符串水题)
time limit per test: 1 second memory limit per test: 256 megabytes input: standard input output: sta ...
- SqlSugar ORM 的学习
http://www.codeisbug.com/Doc/8/1163 https://www.cnblogs.com/sunkaixuan/p/6082664.html
- .NET数据采集
public string GetHttpData(string Url) { string sException = null; string sRslt = null; WebResponse o ...