温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-31 21:30:16 来源:网络 阅读:5757 作者:zhangyah 栏目:编程语言

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("&lt;", "<");
strValue = strValue.Replace("&gt;", ">");
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("&lt;", "<");
strValue = strValue.Replace("&gt;", ">");
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); //显示从服务器获取的结果值
}

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI