第一步、WebServices提供的方法没有变动(需要可查看我的上一篇 Http POST方式)
第二步、为了让WebServices支持GET方式,则需要在Web.config配置文件中增加如下代码
1 <system.web> 2 <webServices> 3 <protocols> 4 <add name="HttpGet"/> 5 <add name="HttpPost"/> 6 </protocols> 7 </webServices> 8 <compilation debug="true" targetFramework="4.6.1"/> 9 <httpRuntime targetFramework="4.6.1"/> 10 </system.web>
第三步、编写Winform客户端HttpGET方法
1 string HttpGet(string URL) 2 { 3 // 创建HttpWebRequest对象 4 HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(URL); 5 httpRequest.Method = "GET"; 6 7 try 8 { 9 using (HttpWebResponse myResponse = (HttpWebResponse)httpRequest.GetResponse()) 10 { 11 StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 12 string responseString = sr.ReadToEnd(); 13 return responseString; 14 } 15 } 16 catch (WebException ex) 17 { 18 var res = (HttpWebResponse)ex.Response; 19 StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8); 20 string str = sr.ReadToEnd(); 21 return str; 22 } 23 }
第四步、设计调用界面
第五步、编写调用方法
1 string url = "http://localhost:6029/TestServ.asmx/PostJson?str=11&bb=AA"; 2 3 textBox1.Text = HttpGet(url);
地六部、执行界面显示结果