c# winform 访问WebServices (通过Http方式)

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

c# winform 访问WebServices (通过Http方式)

一叶孤城   2020-03-26 我要评论

第一步、编写WebServices服务方法

 1         [WebMethod]
 2         public void PostJson(string str, string bb)
 3         {
 4             Dictionary<string, object> DictResult = new Dictionary<string, object>();
 5             IList<Dictionary<string, string>> list = new List<Dictionary<string, string>>();
 6             /*此处提供的只是示例,实际项目中,按照具体业务*/
 7             for (int ii = 0; ii < 5; ii++)
 8             {
 9                 Dictionary<string, string> dict = new Dictionary<string, string>();
10                 dict.Add("str", str);
11                 dict.Add("bb", bb);
12                 list.Add(dict);
13             }
14             /*此处提供的只是示例,实际项目中,按照具体业务*/
15             if (str == "11")
16             {
17                 DictResult.Add("Code", "0");
18                 DictResult.Add("Msg", "访问成功");
19                 DictResult.Add("Result", list);
20             }
21             else
22             {
23                 DictResult.Add("Code", "-1");
24                 DictResult.Add("Msg", "访问失败");
25                 DictResult.Add("Result", null);
26             }
27 
28             Tools.ConvertToJson(DictResult);
29         }

Web方法返回的内容

 

 

 第二步、编写Winform客户端的访问代码

 1  string HttpPost(string URL, string Para)
 2         {
 3             // 创建HttpWebRequest对象
 4             HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(URL);
 5             httpRequest.Method = "POST";
 6             httpRequest.ContentType = "application/x-www-form-urlencoded";
 7             byte[] bytes = Encoding.UTF8.GetBytes(Para);
 8             using (Stream reqStream = httpRequest.GetRequestStream())
 9             {
10                 reqStream.Write(bytes, 0, bytes.Length);
11                 reqStream.Flush();
12             }
13             try
14             {
15                 using (HttpWebResponse myResponse = (HttpWebResponse)httpRequest.GetResponse())
16                 {
17                     StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
18                     string responseString = sr.ReadToEnd();
19                     return responseString;
20                 }
21             }
22             catch (WebException webex)
23             {
24                 var res = (HttpWebResponse)webex.Response;
25                 StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
26                 string str = sr.ReadToEnd();
27                 return str;
28             }
29         }

第三步、设计调用界面

 

 

 

第四步、执行调用方法

1   private void button1_Click(object sender, EventArgs e)
2         {
3             string url = "http://localhost:6029/TestServ.asmx/PostJson";
4             string Para = "str=11&bb=AA";          
5             textBox1.Text = HttpPost(url, Para);
6         }

第五步、显示执行结果

 

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们