在c#中通过http对象编程实现webservice调用

1.webservice帮助类

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

public class WebServiceHelper
    {      
        public static string CallServiceByGet(string strURL)
        {  
            //string strURL = "http://localhost:12074/Service1.asmx/GetProductPrice?ProductId=";
            //strURL += this.textBox1.Text;
            //创建一个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)
        {     
            //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编码
            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://tempuri.org/")]
    [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();

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值