我貼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#代碼創建的時候道理一樣。