成都创新互联网站制作重庆分公司

手动发送HTTP请求调用和异步调用WebService

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对象实现。

/// 
/// 界面的进度条显示
/// 
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); //显示从服务器获取的结果值
}

新闻名称:手动发送HTTP请求调用和异步调用WebService
网址分享:http://cxhlcq.com/article/jhieij.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部