編寫 WebService 程式

2022-09-17 12:45:10 字數 1667 閱讀 6518

webservice 是一種概念嗎?你又是如何理解的呢?

之前,我寫的程式為了讓**和html分開是用的 request 物件,然後switch 一下..webservice soap 神馬的概念一頭霧水,看了webservice的例子之後發現也就是在這個基礎上省了乙個swith 方法,因為它直接把方法生成了url的一部分。

下面寫乙個最簡單的webservice 並指出其中所注意的事項

1.vs2010 不支援 webservice?

新建的時候,把.net framework 4.0 改成3.5 或者一下 新建web服務後再改成 4.0

為了讓他們支援json 要是改成了2.0 就要記得在建立工程後改回來

2.webservice 只能返回xml資料?

在方法上面加上這一句就可以返回xml資料了

[scriptmethod(responseformat = responseformat.json)]

需要新增以下引用

using system.web.script.services;

3.若要允許使用  ajax 從指令碼中呼叫此 web 服務,請取消對下行的注釋。否則ajax 呼叫web服務時會報http 500 錯誤

[system.web.script.services.scriptservice]

寫下如下的**:

using

system;

using

system.collections.generic;

using

system.web;

using

system.web.script.services;

using

system.web.services;

namespace

testservice

[webmethod]

[scriptmethod(responseformat =responseformat.json)]

public user getoneuserinfo(string name, int

age));}

[webmethod]

[scriptmethod(responseformat =responseformat.json)]

public user getusers(string name, int

age)

);res.add(

new user );

return

res.toarray();}}

public

class

user

public

int age

}}

寫下如下的測試 html

1.http post 過去引數的名稱,對應 webservice 函式中引數的名稱,windows 的 webservice 接收不按照順序,按照名稱,而且不區分大小寫

2.伺服器返回的物件直接序列化成json 按照程式所示,可以通過data.d 得到其物件,不知道支不支援多個list的json,可以試試

3.除錯過程的請求時,仍然會有http 500 錯誤,看來只有部署上webservice 才能解決這個問題了

PHP 呼叫 C 編寫的webservice

c 服務端 using system using system.collections.generic using system.linq using system.web using system.web.services using system.data using system.xml us...

程式編寫風格

1.編寫程式時,可以在運算子的左右或是逗號 之後適當地使用一些空白,讓程式看來不那麼擁擠。對比 int i 0 int i 0 2.在宣告字串並指定字串值時,如果字串長度過長,可以分作兩行來寫,比較容易閱讀,例如 string text n n 3.類名首字母大寫 方法名稱的命名慣例為首字母小寫 名...

編寫網路程式

為什麼需要htons ntohl ntohs htons 函式 2009 12 11 13 15 51 分類 在c c 寫網路程式的時候,往往會遇到位元組的網路順序和主機順序的問題。這是就可能用到htons ntohl ntohs htons 這4個函式。網路位元組順序與本地位元組順序之間的轉換函式...