真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

手動發(fā)送HTTP請求調(diào)用和異步調(diào)用WebService

1、手動發(fā)送HTTP請求調(diào)用WebService

創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供郊區(qū)網(wǎng)站建設(shè)、郊區(qū)做網(wǎng)站、郊區(qū)網(wǎng)站設(shè)計、郊區(qū)網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、郊區(qū)企業(yè)網(wǎng)站模板建站服務(wù),十多年郊區(qū)做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。

(1)、Get方式調(diào)用:

string strURL = "http://localhost:12074/Service1.asmx/GetProductPrice?ProductId=";
strURL += this.textBox1.Text;
//創(chuàng)建一個HTTP請求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
//Get請求方式
request.Method="GET";
//獲得響應(yīng)流
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方式調(diào)用:

string strURL = "http://localhost:12074/Service1.asmx/GetProductPrice";
//創(chuàng)建一個HTTP請求
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
//Post請求方式
request.Method = "POST";
//內(nèi)容類型
request.ContentType = "application/x-www-form-urlencoded";
//參數(shù)經(jīng)過URL編碼
string paraUrlCoded = HttpUtility.UrlEncode("ProductId");
paraUrlCoded += "=" + HttpUtility.UrlEncode(this.textBox1.Text);
byte[] payload;
//將URL編碼后的字符串轉(zhuǎn)化為字節(jié)
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
//設(shè)置請求的ContentLength
request.ContentLength = payload.Length;
//獲得請求流
Stream writer = request.GetRequestStream();
//將請求參數(shù)寫入流
writer.Write(payload, 0, payload.Length);
//關(guān)閉請求流
writer.Close();
//獲得響應(yīng)流
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、異步調(diào)用WebService

(1)、利用Backgroundworker對象實現(xiàn)。

/// 
/// 界面的進(jìn)度條顯示
/// 
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);
// 注冊調(diào)用完成后的回調(diào)方法
backgroundworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(back_RunWorkerCompleted);
// 這里開始異步調(diào)用
backgroundworker.RunWorkerAsync();
//調(diào)用服務(wù)的同時客戶端處理并不停止
ChangeProcessBar();
}
//完成事件
void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
throw e.Error;
progressBar1.Value = progressBar1.Maximum; //調(diào)用完成了,把客戶端進(jìn)度條填充滿
string price = e.Result.ToString();   //獲取處理結(jié)果
MessageBox.Show("調(diào)用完成。價格是:" + price); //顯示從服務(wù)器獲取的結(jié)果值
}
//調(diào)用方法
void back_DoWork(object sender, DoWorkEventArgs e)
{
//Web Service代理類
ProductService.LTPService service = new ProductService.LTPService();
// 調(diào)用Web方法GetProductPrice,結(jié)果賦值給DoWorkEventArgs的Result對象
e.Result = service.GetProductPrice("001");
}

(2)、利用WebService的webMethod中的Async方法實現(xiàn)。

private void button2_Click(object sender, EventArgs e)
{
ProductService.LTPService service = new ProductService.LTPService();//Web Service代理類
//這里開始異步調(diào)用
service.GetProductPriceAsync("001");
// 注冊調(diào)用完成后的回調(diào)方法
service.GetProductPriceCompleted += new ProductService.GetProductPriceCompletedEventHandler(GetProductPriceCompleted);
//調(diào)用同時客戶端處理不停止
ChangeProcessBar();
}
//完成事件
void GetProductPriceCompleted(object sender, ProductService.GetProductPriceCompletedEventArgs e)
{
if (e.Error != null)
throw e.Error;
progressBar1.Value = progressBar1.Maximum; //調(diào)用完成了,把客戶端進(jìn)度條填充滿
string price = e.Result.ToString(); //獲取處理結(jié)果
MessageBox.Show("調(diào)用完成。價格是:" + price); //顯示從服務(wù)器獲取的結(jié)果值
}

本文名稱:手動發(fā)送HTTP請求調(diào)用和異步調(diào)用WebService
網(wǎng)頁URL:http://weahome.cn/article/jhieij.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部