您好,登錄后才能下訂單哦!
1、手動發送HTTP請求調用WebService
(1)、Get方式調用:
string strURL = "http://localhost:12074/Service1.asmx/GetProductPrice?ProductId="; strURL += this.textBox1.Text; //創建一個HTTP請求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); //Get請求方式 request.Method="GET"; //獲得響應流 HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse(); Stream s = response.GetResponseStream(); XmlTextReader Reader = new XmlTextReader(s); Reader.MoveToContent(); string strValue = Reader.ReadInnerXml(); strValue = strValue.Replace("<", "<"); strValue = strValue.Replace(">", ">"); MessageBox.Show(strValue); Reader.Close();
(2)、Post方式調用:
string strURL = "http://localhost:12074/Service1.asmx/GetProductPrice"; //創建一個HTTP請求 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL); //Post請求方式 request.Method = "POST"; //內容類型 request.ContentType = "application/x-www-form-urlencoded"; //參數經過URL編碼 string paraUrlCoded = HttpUtility.UrlEncode("ProductId"); paraUrlCoded += "=" + HttpUtility.UrlEncode(this.textBox1.Text); byte[] payload; //將URL編碼后的字符串轉化為字節 payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); //設置請求的ContentLength request.ContentLength = payload.Length; //獲得請求流 Stream writer = request.GetRequestStream(); //將請求參數寫入流 writer.Write(payload, 0, payload.Length); //關閉請求流 writer.Close(); //獲得響應流 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream s = response.GetResponseStream(); XmlTextReader Reader = new XmlTextReader(s); Reader.MoveToContent(); string strValue = Reader.ReadInnerXml(); strValue = strValue.Replace("<", "<"); strValue = strValue.Replace(">", ">"); MessageBox.Show(strValue); Reader.Close();
2、異步調用WebService
(1)、利用Backgroundworker對象實現。
/// <summary> /// 界面的進度條顯示 /// </summary> void ChangeProcessBar() { for (int i = 0; i < 10; i++) { progressBar1.Value = i; System.Threading.Thread.Sleep(500); } } private void button1_Click(object sender, EventArgs e) { BackgroundWorker backgroundworker = new BackgroundWorker(); // 注冊具體異步處理的方法 backgroundworker.DoWork += new DoWorkEventHandler(back_DoWork); // 注冊調用完成后的回調方法 backgroundworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(back_RunWorkerCompleted); // 這里開始異步調用 backgroundworker.RunWorkerAsync(); //調用服務的同時客戶端處理并不停止 ChangeProcessBar(); } //完成事件 void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Error != null) throw e.Error; progressBar1.Value = progressBar1.Maximum; //調用完成了,把客戶端進度條填充滿 string price = e.Result.ToString(); //獲取處理結果 MessageBox.Show("調用完成。價格是:" + price); //顯示從服務器獲取的結果值 } //調用方法 void back_DoWork(object sender, DoWorkEventArgs e) { //Web Service代理類 ProductService.LTPService service = new ProductService.LTPService(); // 調用Web方法GetProductPrice,結果賦值給DoWorkEventArgs的Result對象 e.Result = service.GetProductPrice("001"); }
(2)、利用WebService的webMethod中的Async方法實現。
private void button2_Click(object sender, EventArgs e) { ProductService.LTPService service = new ProductService.LTPService();//Web Service代理類 //這里開始異步調用 service.GetProductPriceAsync("001"); // 注冊調用完成后的回調方法 service.GetProductPriceCompleted += new ProductService.GetProductPriceCompletedEventHandler(GetProductPriceCompleted); //調用同時客戶端處理不停止 ChangeProcessBar(); } //完成事件 void GetProductPriceCompleted(object sender, ProductService.GetProductPriceCompletedEventArgs e) { if (e.Error != null) throw e.Error; progressBar1.Value = progressBar1.Maximum; //調用完成了,把客戶端進度條填充滿 string price = e.Result.ToString(); //獲取處理結果 MessageBox.Show("調用完成。價格是:" + price); //顯示從服務器獲取的結果值 }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。