c# JSON返回格式的WEB SERVICE

2016-02-19 09:36 4 1 收藏

最近很多朋友喜欢上设计,但是大家却不知道如何去做,别担心有图老师给你解答,史上最全最棒的详细解说让你一看就懂。

【 tulaoshi.com - 编程语言 】

我贴c#的代码:
代码如下:

namespace IWebs.Webs{
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.XPath;
using System.Web.Script.Services;
using IWebs;
[WebService (Name="cjjer",Description="一个返回用户资料,订单信息的WebService,请求的手机号码最长12位",Namespace="http://www.cjjer.com/webs/")]
[System.Web.Script.Services.ScriptService]
public class cjjer:WebService{
public class ReqHeader : SoapHeader{
public string userName;
public string password;
}
public ReqHeader header;
[WebMethod (Description ="输入单个用户的int值ID,返回用户类",MessageName="GetUser",EnableSession = false)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[SoapHeader("header", Direction = SoapHeaderDirection.In)]
public Model.Member GetUser(int uid){
this.ChechHeader(header);
return (new DAL.Members()).GetById(uid);
}
[WebMethod (Description ="输入某个用户的手机号码,返回用户类",MessageName="GetUserByMobile",EnableSession = false)]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
[SoapHeader("header", Direction = SoapHeaderDirection.In)]
public Model.Member GetUserByMobile(string umobile){
this.ChechHeader(header);
return (new DAL.Members()).GetByMobile(umobile);
}
[WebMethod (Description ="输入某个用户的手机号码,返回订单数组",MessageName="GetOrdersByMobile",EnableSession = false)]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
[SoapHeader("header", Direction = SoapHeaderDirection.In)]
public IWebs.Model.Order[] GetOrdersByMobile(string umobile){
this.ChechHeader(header);
return (new DAL.Orders()).GetByMobile(umobile,-365);
}
[WebMethod (Description ="输入某个用户的ID,返回订单数组",MessageName="GetOrdersByUserId",EnableSession = false)]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
[SoapHeader("header", Direction = SoapHeaderDirection.In)]
public IWebs.Model.Order[] GetOrdersByUserId(int uid){
this.ChechHeader(header);
return (new DAL.Orders()).GetOrdersByUserId(uid,-365);
}
private void ChechHeader(ReqHeader header){
if (header != null){
if (header.MustUnderstand)
{
string UserName = header.userName;
string PassWord = header.password;
if (UserName == "cjjer" && PassWord == "000000")
{
return ;
}
else
{
throw new ApplicationException (String.Format("用户名[{0}]或密码[{1}]错误",UserName,PassWord));
}
}
else
{
throw new ApplicationException ("包含用户名和密码信息的消息头格式不正确");
}
}
else
{
throw new ApplicationException ("请提交包含用户名和密码信息的消息头");
}
}
};
}

注意的是,这个请求必须要请求提交SoapHeader,其中的[System.Web.Script.Services.ScriptService]
这句是利用AJAX.NET处理JSON请求的,如果不需要就免了,如果需要的话下载AJAX.NET,然后在BIN里面放System.Web.Extensions.Design.dll,System.Web.Extensions.dll,按照AJAX.NET默认的那个WEB.CONFIG修改你的web.config,在浏览器中查看*.ASMX文件,如果使用?wsdl可以看到xml的wsdl的话第一步算成功了。
其中注意的是:
Web.Config
在httpHandler中有两个节点很重要:
代码如下:

remove verb="*" path="*.asmx"/
add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/

这两句声明让ScriptHandlerFactory处理WebService请求。
利用ajax请求的时候 http_request.setRequestHeader("Content-Type", "application/json");
加上这句默认的返回的就是JSON。
附上web.CONFIG和相关的dll文件吧:
c# json
在c#代码创建的时候道理一样。

来源:https://www.tulaoshi.com/n/20160219/1591412.html

延伸阅读
标签: Web开发
一、AjaxJson.aspx 处理业务数据,产生JSon数据,供JqueryRequest.aspx调用,代码如下: 代码如下: protected void Page_Load(object sender, EventArgs e) { string u = Request["UserName"]; string p = Request["Password"]; string output = string.Format("'UserName':'{0}','Password':'{1}'", u, p); Response.Write("[{...
标签: Web开发
c# JSON返回格式的WEB SERVICEhttp://www.jb51.net/article/16768.htm 我这里是利用axis创建类包的,首先下载axis的压缩包,然后解压,把axis放到webapp下面。如果可以运行,就OK了。   c#创建的webservice自带的wsdl的,直接可以加上?wsdl访问,然后利用axis生成类: java -classpath ".;commons-logging.jar;axis.jar;co...
标签: Web开发
XML Web Service 是在 Internet 上进行分布式计算的基本构造块。开放的标准以及对用户和应用程序之间的通信和协作的关注产生了这样一种环境,在这种环境下,XML Web Service 成为应用程序集成的平台。应用程序是通过使用多个不同
为了完成以上的需求,我们就需要模拟浏览器浏览网页,得到页面的数据在进行分析,最后把分析的结构,即整理好的数据写入数据库。那么我们的思路就是: 1、发送HttpRequest请求。 2、接收HttpResponse返回的结果。得到特定页面的html源文件。 3、取出包含数据的那一部分源码。 4、根据html源码生成HtmlDocument,循环取出数...
新建Visual C#项目ASP.NET Web应用程序,将下列代码插入PageLoad事件处理函数中: OWC.ChartSpace objCSpace = new OWC.ChartSpaceClass(); OWC.WCChart objChart = objCSpace.Charts.Add(0); objChart.Type = OWC.ChartChartTypeEnum.chChartTypeColumnClustered; objChart.HasLegend = true; objChart.HasTitle = true; objChart.Title.Capt...

经验教程

523

收藏

15
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部