webservice使用基本技巧

來源:互聯網
上載者:User

標籤:擷取   方法   環境   name   sci   通過   try   返回   web   

一,webService基本概念

webService也叫XMLWeb SerVice WebService是一種可以接收從Internet或者Intranet上的其它系統中傳遞過來的請求,輕量級的獨立通訊技術。是:通過SOAP在Web上提供的軟體(服務),使用WSDL檔案進行(說明),並通過(UDDI)進行註冊。

XML:(Extensible Markup Language)擴充型可標記語言。面向短期的臨時資料處理、面向全球資訊網絡,是Soap的基礎。

Soap:(Simple Object Access Protocol)簡單對象存取協議。是XML Web Service 的通訊協定。當使用者通過UDDI找到你的WSDL描述文檔後,他通過可以SOAP調用你建立的Web服務中的一個或多個操作。SOAP是XML文檔形式的調用方法的規範,它可以支援不同的底層介面,像HTTP(S)或者SMTP。

WSDL:(Web Services Description Language) WSDL 檔案是一個 XML 文檔,用於說明一組 SOAP 訊息以及如何交換這些訊息。大多數情況下由軟體自動產生和使用。

UDDI (Universal Description, Discovery, and Integration) 是一個主要針對Web服務供應商和使用者的新項目。在使用者能夠調用Web服務之前,必須確定這個服務內包含哪些商務方法,找到被調用的介面定義,還要在服務端來編製軟體,UDDI是一種根據描述文檔來引導系統尋找相應服務的機制。UDDI利用SOAP訊息機制(標準的XML/HTTP)來發布,編輯,瀏覽以及尋找註冊資訊。它採用XML格式來封裝各種不同類型的資料,並且發送到註冊中心或者由註冊中心來返回需要的資料。

 http soap之間的關係

http:是一個用戶端和伺服器端請求和應答的標準(TCP)。http協議其目的是為了提供一種發布和接收http頁面的方法。

http協議的用戶端與伺服器的互動:由HTTP用戶端發起一個請求,建立一個到伺服器指定連接埠(預設是80連接埠)的TCP串連。HTTP伺服器則在那個連接埠監聽用戶端發送過來的請求。一旦收到請求,伺服器(向用戶端)發回一個狀態行,比如"HTTP/1.1 200 OK",和(響應的)訊息,訊息的訊息體可能是請求的檔案、錯誤訊息、或者其它一些資訊。

soap 協議:它描述了一種在分散或分布式的環境中如何交換資訊的輕量級協議。soap在http協議的基礎上,一個基於XML的協議。

不同:都是底層的通訊協定,請求包的格式不同而已,soap包是XML格式,http純文字格式。

關係:SOAP是個通訊協定, SOAP在HTTP協議的基礎上,把編寫成XML的REQUEST參數, 放在HTTP BODY上提交個WEB SERVICE伺服器(SERVLET,ASP什麼的) 處理完成後,結果也寫成XML作為RESPONSE送回使用者端, 為了使使用者端和WEB SERVICE可以相互對應,可以使用WSDL作為這種通訊方式的描述檔案,利用WSDL工具可以自動產生WS和使用者端的架構檔案,SOAP具備把複雜物件序列化捆綁到XML裡去的能力。

二,C#實現webservice通訊的基本方法

1.引用using system.net 使用內建的 HttpWebRequest與HttpWebresponse實現。這種方法少先複雜。

HttpWebRequest類主要利用Http協議和伺服器互動,通常是通過Get和Post兩種方式來對資料進行擷取與提交;

Get方式:

post方式Get 方式通過在網路地址附加參數來完成資料的提交,比如在地址http://www.jb51.net/?hl=zh-CN 中,前面部分 http://www.jb51.net表示資料提交的網址,後面部分 hl=zh-CN 表示附加的參數,其中 hl 表示一個鍵(key), zh-CN 表示這個鍵對應的值(value)。

c#代碼如下:

HttpWebRequest wrequest = (HttpWebRequest)WebRequest.Create( "http://www.jb51.net?hl=zh-CN" );
wrequest.Method = "GET";
HttpWebResponse webResponse = (HttpWebResponse)wrequest.GetResponse();
StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8);
string data= sr.ReadToEnd();
return data;

另:發送帶參數

 

protected ResultInfo AddInstrumetStatus(string instrument_id, string status, string message)
{

try
{
string postDataStr = instrument_id + "&" + status + "&" + message;
string URL =Url + (postDataStr == "" ? "" : "?") + postDataStr;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URL);
myRequest.Method = "POST";
myRequest.ContentType = "application/json;charset=UTF-8";
myRequest.MediaType = "application/json";
myRequest.Accept = "application/json";
if (!string.IsNullOrEmpty(this._userName) && !string.IsNullOrEmpty(this._password))
{
myRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(this._userName + ":" + this._password)));
}
//接收資料
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
if (myResponse.StatusCode == HttpStatusCode.Created)
{
result.IsSuccess = true;
using (Stream s = myResponse.GetResponseStream())
{
StreamReader myStreamReader = new StreamReader(s, Encoding.UTF8);
result.ResultData = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponse.Close();
s.Close();
myRequest.Abort();
}
}

else if (myResponse.StatusCode == HttpStatusCode.OK)
{
//result.ValidateMessages = new Dictionary<string, string>();
//fill valid messages from response content

}
else
{
result.ResultMessage = String.Format("GetTaskConfig failed {0}", myResponse.StatusCode);
}
}
catch (Exception ex)
{
throw new Exception("the zlims api connected exception, " + ex.ToString());
}
return result;

post方式:

 

String Strjson;//發送的資料
byte[] payload = System.Text.Encoding.UTF8.GetBytes(Strjson);
ResultInfo result = new ResultInfo();
try
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url);
myRequest.Method = "POST";
myRequest.ContentType = "application/json;charset=UTF-8";
myRequest.MediaType = "application/json";
myRequest.Accept = "application/json";
if (!string.IsNullOrEmpty(this._userName) && !string.IsNullOrEmpty(this._password))
{
myRequest.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(this._userName + ":" + this._password)));
}
myRequest.ContentLength = payload.Length;
//發送資料
using (Stream Writer = myRequest.GetRequestStream())
{
Writer.Write(payload, 0, payload.Length);
Writer.Close();
}
//接收資料
HttpWebResponse myResponse;
myResponse = (HttpWebResponse)myRequest.GetResponse();
if (myResponse != null)
{
result.IsSuccess = true;
using (Stream s = myResponse.GetResponseStream())
{
StreamReader myStreamReader = new StreamReader(s, Encoding.UTF8);
result.ResultData = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponse.Close();
s.Close();
myRequest.Abort();
}
}

else if (myResponse.StatusCode == HttpStatusCode.OK)
{

}
else
{
result.ResultMessage = String.Format("GetTaskConfig failed {0}", myResponse.StatusCode);
}
}
catch (Exception ex)
{
throw new Exception("the zlims api connected exception, " + ex.ToString());
}
return result;

}

webservice使用基本技巧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.