顯示具有 Web Service 標籤的文章。 顯示所有文章
顯示具有 Web Service 標籤的文章。 顯示所有文章

Web Service界接-如何通過Web Service帳號驗證並取得資料

寫在前面:
1.本測試使用前文架設之Web Service,request抽水站名稱,response抽水站地址,且要通過帳號驗證
2.開發工具使用VS2008
3.本文重點在於如何傳遞帳號並通過驗證,並進一步呼叫WebMethod取回data
4.以一般網頁介面,予user直接輸入資訊進行測試

SETP 1:新增一ASP .NET  Web應用程式
新增專案

專案初始狀態 


SETP 2:加入Web參考
在'URL'中輸入服務所在網址,按下'移至'搜尋服務,搜尋完畢後賦予其Web 參考名稱Hello(預設為localhost),最後按下'加入參考'完成網路服務的include!
引入後續採用之服務

加入參考後的專案結構


SETP 3:觀察SOAP包裝的request
POST /ws/soapheader/service1.asmx HTTP/1.1
Host: localhost Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://tempuri.org/HelloWorld" < ?xml version="1.0" encoding="utf-8"?> < soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
< soap:Header> < HeoSoapHeader xmlns="http://tempuri.org/">
< userid>string< /userid>
< password>string< /password>
< /HeoSoapHeader>
< /soap:Header>
< soap:Body>
< HelloWorld xmlns="http://tempuri.org/">
< pumpName>string
< /pumpName>
< /HelloWorld> < /soap:Body>
< /soap:Envelope>
由上可知我們必須傳第3項資訊,userid(帳號)、password(密碼)、pumpName(抽水站名稱)


SETP 4:設計網頁
[
[網頁設計說明]
三個TextBox讓user輸入帳號密碼以及抽水站名稱
call HelloWorld按鈕(Button1)可叫用Hello服務
Label1呈現服務回傳結果


SETP 5:撰寫Button1_Click事件,以叫用服務
protected void Button1_Click(object sender, EventArgs e)
{
  Hello.HeoSoapHeader MyHeader = new Hello.HeoSoapHeader();
  Hello.Service1 MyWebServ=new Hello.Service1() ;
  MyHeader.userid = TextBox1.Text ;
  MyHeader.password = TextBox2.Text;
  MyHeader.MustUnderstand = true;
  MyWebServ.HeoSoapHeaderValue = MyHeader;
  string queryResult = MyWebServ.HelloWorld(TextBox3.Text);
  Label1.Text = queryResult;
}


SETP 6:測試
成功取回抽水站地址


-----------------------------本文目標已達成---------------------
閱讀前言、附有權限之Web Service以及本文這一系列文章,可以清楚明白Web Service界接之原理與應用,但目前測試環境全都部署在本機端,之後將再以另篇專文說明,如何處理Web SErvice跨網域之代理伺服器設置。

Web Service界接-如何架設附有權限之Web Service

寫在前面:
1.本測試使用傳統的Web Service,暫不涉獵WCF。
2.開發工具使用VS2008

SETP 1:新增一ASP .NET  Web  Service應用程式
新增專案

專案初始狀態


SETP 2:對Service1.asmx按右鍵選擇『在瀏覽器中檢視』
 預設WebMethod-HelloWorld

 觀察SOAP如何描述服務

由上圖可看出,HelloWorld之SOAP目前只有SOAPBody ,request無須傳值,response回傳string。接下來實作一個需要通過帳號驗證的HelloWorld,設計方式以SOAPHeader包裝。


SETP 3:設計SOAPHeader(在Service1.asmx.cs撰寫)
﹡引入適當namespace using System.Web.Services.Protocols;
﹡定義一個繼承SoapHeader類別的Class
public class HeoSoapHeader : SoapHeader
{
  public string userid;
  public string password;
}

﹡將前述自訂之SoapHeader類別(HeoSoapHeader)指定給HelloWorld()

﹡Complie專案後,於瀏覽器中檢視服務並觀察SOAP
加入自訂SOAPHeader的新HelloWorld
由上圖可看出多了< soap:Header >之tag,其中指定的userid與password則是自定義HeoSoapHeader中的兩個屬性!

﹡繼續完成結合SOAPHeader的HelloWorld
public string HelloWorld()
{
  if (heosoapheader != null)
  {
    if (heosoapheader.userid == "admin" && heosoapheader.password == "P@ssw0rd")
    {
      return "Hello!";
    }else return "驗證失敗。";
  } return "未指定驗證身份。";
}

﹡Compile專案後,叫用HelloWorld()方法
 叫用HelloWorld結果
一般Web測試提供的參數輸入介面,只有WebMethod定義之參數,不包含SOAPHeader裡面的資訊,因此呼叫HelloWorld時會因缺少帳號資訊,無法成功Hello。

至此已完成"如何架設附有權限之Web Service",一旦呼叫時未一併給予SoapHeader資訊,就會因帳號驗證失敗,而回傳WebMethod自定義的錯誤訊息,無法進一步取得想要的data。
-----------------------------本文目標已達成---------------------
----想隨piggy作''如何通過Web Service帳號驗證並取得資料'',請續讀----
改寫一下前述的簡易版HelloWorld(),讓此WebMethod能連接後端DB,確實提供查詢並回傳資料,進行下一篇測試也比較有fu。以下結合LINQ技術作O/R Mapping,將HelloWorld()改為輸入''抽水站名稱'',回傳''抽水站地址''的WebMethod。


SETP 4:改寫HelloWorld()方法
LINQ非本文重點,直接來完整程式碼,給你個痛快XD
public string HelloWorld(string pumpName)
{
  if
(heosoapheader != null)
  {      
    if
(heosoapheader.userid == "admin" && heosoapheader.password == "P@ssw0rd")         {                 pumpstationDataContext context = new pumpstationDataContext();
        var result = (from p in context.pumpstation where p.NAME_C.Contains(name) select p);                 if (result.ToList().Count > 0)                   {           return result.First().ADDR_C;         }else return "查無此抽水站!";
     }else return "帳號密碼錯誤";
}else return "未指定帳號密碼";
}

Web Service界接-前言

合作業主中有位對coding特別有興趣,前些日子提出界接Web Service需求,不過也僅止於有興趣吧,不然piggy也不會進行這項測試XDD

本次任務:
1.使用第三方發佈之WebService(url已知)
2.透過帳號存取權限內的資料(帳密已知)

遭遇情境:
1.在對方只給url情況下,一切資訊只能由WSDL取得
2.WebService帶有帳號驗證,有的Developer寫在WebMethod(不建議),有的以SOAPHeader建構,界接方法因此而異。
此次使用之Web Service採正規方式賦予資料存取權限(即SOAPHeader),在Web測試介面中,沒有所謂的帳號&密碼input box,無法於界接前先行測試,即使wcftestclient(WCF測試用戶端)都無法滿足此需求。

測試結果:
1.傳遞帳號/密碼予Web Service後,無法進行其他後續操作。
HTTP 狀態 407 : Proxy Authentication Required ( The ISA Server requires authorization to fulfill the request. Access to the Web Proxy service is denied. )。
2.帳號/密碼通過驗證後已可進行其他操作 ,操作卻面臨逾時狀況。
已超過連接逾時的設定。在作業完成之前超過逾時等待的時間,或者是伺服器未回應。

解決辦法:
1.原因在於piggy公司內部設有proxy,外部服務會被擋掉,只要明確給定proxy代理資訊即可。
2.突破proxy後只剩單純引用Web Method來收取資料,但無論資料量多小的操作皆面臨逾時的狀況,即使設定client端timeout時間亦無法解決。
考量server端各類狀況都有可能造成影響,在無法實際操作server現實面下,piggy決定重新架設一樣功能的Web Service進行完整測試!

綜合以上,接下來提供兩篇專文描述如何架設附有權限之Web Service、 如何通過Web Service帳號驗證並取得資料。