C#WebService 客户端通过Http调用请求(转)

时间:2021-01-04 05:57:41

1.webservice帮助类

public class WebServiceHelper
    {      
        public static string CallServiceByGet(string strURL)
        {  
           
            //创建一个HTTP请求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Method="get";
            HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
            Stream s = response.GetResponseStream();
            //转化为XML,自己进行处理
            XmlTextReader Reader = new XmlTextReader(s);
            Reader.MoveToContent();
            string strValue = Reader.ReadInnerXml();
            Reader.Close();
            strValue = strValue.Replace("<", "<");
            strValue = strValue.Replace(">", ">");
            return strValue;
        }
        public static string CallServiceByPost(string strURL,System.Collections.Specialized.StringDictionary parameters)
        {         
            //创建一个HTTP请求
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strURL);
            //Post请求方式
            request.Method = "POST";
            //内容类型
            request.ContentType = "application/x-www-form-urlencoded";
            //设置参数,并进行URL编码
            StringBuilder codedString = new StringBuilder();
            foreach (string key in parameters.Keys)
            {
                codedString.Append(HttpUtility.UrlEncode(key));
                codedString.Append("=");
                codedString.Append(HttpUtility.UrlEncode(parameters[key]));
                codedString.Append("&");
            }
           
string paraUrlCoded = codedString.Length == 0 ?
string.Empty:codedString.ToString().Substring(0, codedString.Length -
1);
            //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();
            //转化为XML,自己进行处理
            XmlTextReader Reader = new XmlTextReader(s);
            Reader.MoveToContent();
            string strValue = Reader.ReadInnerXml();
            Reader.Close();
            strValue = strValue.Replace("<", "<");
            strValue = strValue.Replace(">", ">");            
            return strValue;
        }  
    }

---------------------------------------------------------------------------------------------------------------

2.webservice方法

---------------------------------------------------------------------------------------------------------------

/// <summary>
    /// Service1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://127.0.0.1/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

[WebMethod]
        public string HelloWorld1()
        {
            return "Hello World";
        }

[WebMethod]
        public string HelloWorld2(string p1,string p2)
        {
            return string.Concat("Hello World", ",", p1, ",", p2);
        }
        [WebMethod]
        public string HelloWorld3(string datetime)
        {
            return string.Concat("Hello World", " today is ", datetime);
        }
    }

---------------------------------------------------------------------------------------------------------------

3.使用webservice帮助类调用webservice方法

---------------------------------------------------------------------------------------------------------------

//带2个字符串参数的调用
            string url = "http://localhost/ByDIF.WebService/Service.asmx/HelloWorld2";
            StringDictionary parameters = new StringDictionary();
            parameters.Add("p1","123");
            parameters.Add("p2", "456");
            string message = WebServiceHelper.CallServiceByPost(url,parameters);
            System.Console.WriteLine(message);
            //不带参数的调用
            url = "http://localhost/ByDIF.WebService/Service.asmx/HelloWorld1";
            parameters = new StringDictionary();
            message = WebServiceHelper.CallServiceByPost(url, parameters);
            System.Console.WriteLine(message);
            //带一个日期字符串的调用
            url = "http://localhost/ByDIF.WebService/Service.asmx/HelloWorld3";
            parameters = new StringDictionary();
            parameters.Add("datetime", System.DateTime.Now.ToShortDateString());
            message = WebServiceHelper.CallServiceByPost(url, parameters);
            System.Console.WriteLine(message);
            System.Console.Read();