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