webservice它是一種構建應用程式的普遍模型,可以在任何支援網路通訊的作業系統中實施運行;它是一種新的web webservice
應用程式分支,是自包含、自描述、模組 化的應用,發行就緒、定位、通過web調用。web service是一個應用組件,它邏輯性的為其他應用程式提供資料與服務.各應用程式通過網路通訊協定和規定的一些標準資料格式(http,xml,soap)來訪問web service,通過web service內部執行得到所需結果.web service可以執行從簡單的請求到複雜商務處理的任何功能。一旦部署以後,其他web service應用程式可以發現並調用它部署的服務。
using system;
using system.io;
using system.collections;
using system.collections.generic;
using system.componentmodel;
using system.web;
using system.web.services;
using system.web.services.protocols;
using system.drawing;
using system.drawing.imaging;
namespace nightkidsservices
{
/// <summary>
/// service1 的摘要說明
/// </summary>
[webservice(namespace = "http://tempuri.org/")]
[webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
[toolboxitem(false)]
public class testservice :webservice
{
private static int picnum = -1;
[webmethod]
public resource getresource()
{
return resource.createresource("pic2", "asdfasd", 0);
}
[webmethod]
public string helloworld()
{
return "hello";
}
[webmethod]
public byte[] getpic()
{
picnum = (picnum + 1) % 32;
image image = image.fromfile(this.server.mappath("jpeg/" + (picnum+1).tostring() + ".bmp"));
memorystream mem=new memorystream();
image.save(mem, imageformat.jpeg);
return mem.getbuffer();
}
[webmethod]
public list<resource> getresourcelist()
{
return new list<resource>(new resource[] { resource.createresource("pic1", "jpeg/1.bmp", 0),resource.createresource("pic2", "jepg/2.bmp", 0), resource.createresource("pic3", "jpeg/3.bmp", 0), resource.createresource("pic4", "jepg/4.bmp", 0) });
}
}
以上只是一個簡單的測試使用,便於後續使用網頁特效處理不同類型的資料
對於javascript,肯定是使用xmlhttprequest對象來訪問伺服器端的,不過這裡為了簡單,我沒有考慮相容性問題,直接使用xmlhttprequest對象(我使用chrome瀏覽器作為測試瀏覽器),為此我使用ajaxclient類來進行http操作(post 方法),webservice類來封裝處理webservice(調用ajaxclient類作為操作類),jsonconverter類處理xml資料轉換為json資料
common.js(包含jsonconverter類)