c# JSON返回格式的WEB SERVICE
更新時間:2008年12月10日 13:14:34 作者:
首先用c#創(chuàng)建一個web service,主要是利用其WSDL的功能,當(dāng)然也可以利用php創(chuàng)建一個,道理都是一樣的
我貼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 ="輸入某個用戶的手機號碼,返回訂單數(shù)組",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,返回訂單數(shù)組",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 ("請?zhí)峤话脩裘兔艽a信息的消息頭");
}
}
};
}
注意的是,這個請求必須要請求提交SoapHeader,其中的[System.Web.Script.Services.ScriptService]
這句是利用AJAX.NET處理JSON請求的,如果不需要就免了,如果需要的話下載AJAX.NET,然后在BIN里面放System.Web.Extensions.Design.dll,System.Web.Extensions.dll,按照AJAX.NET默認(rèn)的那個WEB.CONFIG修改你的web.config,在瀏覽器中查看*.ASMX文件,如果使用?wsdl可以看到xml的wsdl的話第一步算成功了。
其中注意的是:
Web.Config
在httpHandler中有兩個節(jié)點很重要:
<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");
加上這句默認(rèn)的返回的就是JSON。
附上web.CONFIG和相關(guān)的dll文件吧:
c# json
在c#代碼創(chuàng)建的時候道理一樣。
復(fù)制代碼 代碼如下:
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 ="輸入某個用戶的手機號碼,返回訂單數(shù)組",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,返回訂單數(shù)組",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 ("請?zhí)峤话脩裘兔艽a信息的消息頭");
}
}
};
}
注意的是,這個請求必須要請求提交SoapHeader,其中的[System.Web.Script.Services.ScriptService]
這句是利用AJAX.NET處理JSON請求的,如果不需要就免了,如果需要的話下載AJAX.NET,然后在BIN里面放System.Web.Extensions.Design.dll,System.Web.Extensions.dll,按照AJAX.NET默認(rèn)的那個WEB.CONFIG修改你的web.config,在瀏覽器中查看*.ASMX文件,如果使用?wsdl可以看到xml的wsdl的話第一步算成功了。
其中注意的是:
Web.Config
在httpHandler中有兩個節(jié)點很重要:
復(fù)制代碼 代碼如下:
<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");
加上這句默認(rèn)的返回的就是JSON。
附上web.CONFIG和相關(guān)的dll文件吧:
c# json
在c#代碼創(chuàng)建的時候道理一樣。
您可能感興趣的文章:
- C#發(fā)送HttpPost請求來調(diào)用WebService的方法
- C#動態(tài)webservice調(diào)用接口
- c#動態(tài)調(diào)用Webservice的兩種方法實例
- C# .Net動態(tài)調(diào)用webService實現(xiàn)思路及代碼
- asp.net(c#)動態(tài)修改webservice的地址和端口(動態(tài)修改配置文件)
- c#動態(tài)改變webservice的url訪問地址
- c#編寫webservice服務(wù)引用實例分享
- C#使用PHP服務(wù)端的Web Service通信實例
- C# WebService發(fā)布以及IIS發(fā)布
- C#使用Http Post方式傳遞Json數(shù)據(jù)字符串調(diào)用Web Service
相關(guān)文章
Unity UI組件ScrollRect實現(xiàn)無限滾動條
這篇文章主要為大家詳細(xì)介紹了Unity UI組件ScrollRect實現(xiàn)無限滾動條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-07-07C#調(diào)用FFplay實現(xiàn)播放視頻功能
這篇文章主要為大家詳細(xì)介紹了C#如何調(diào)用FFplay實現(xiàn)播放視頻功能,文中的示例代碼講解詳細(xì),具有一定的參考價值,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10Winform項目中使用FastReport.Net報表控件
這篇文章介紹了Winform項目中使用FastReport.Net報表控件的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06詳解Unity使用ParticleSystem粒子系統(tǒng)模擬藥水在血管中流動(粒子碰撞)
這篇文章主要介紹了Unity使用ParticleSystem粒子系統(tǒng)模擬藥水在血管中流動(粒子碰撞),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-05-05