最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#中使用HttpPost調(diào)用WebService的方法

 更新時(shí)間:2022年03月26日 11:32:10   作者:.NET開發(fā)菜鳥  
這篇文章介紹了C#中使用HttpPost調(diào)用WebService的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

WebService服務(wù)端代碼

public class WebServiceDemo : System.Web.Services.WebService
    {

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

        [WebMethod]
        public string Sum(string param1, string param2)
        {
            int num1 = Convert.ToInt32(param1);
            int num2 = Convert.ToInt32(param2);

            int sum = num1 + num2;

            return sum.ToString();
        }
    }

客戶端調(diào)用代碼

class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            string url = "http://localhost:12544/WebServiceDemo.asmx";
            string method = "Sum";
            string num1 = "1";
            string num2 = "2";

            string result = program.HttpPostWebService(url, method, num1, num2);

            Console.WriteLine(result);
            Console.ReadKey();
        }

        public string HttpPostWebService(string url,string method,string num1,string num2)
        {
            string result = string.Empty;
            string param = string.Empty;
            byte[] bytes = null;

            Stream writer = null;
            HttpWebRequest request = null;
            HttpWebResponse response = null;

            param = HttpUtility.UrlEncode("param1") + "=" + HttpUtility.UrlEncode(num1) + "&" + HttpUtility.UrlEncode("param2") + "=" + HttpUtility.UrlEncode(num2);
            bytes = Encoding.UTF8.GetBytes(param);

            request = (HttpWebRequest)WebRequest.Create(url + "/" + method);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = bytes.Length;

            try
            {
                writer = request.GetRequestStream();        //獲取用于寫入請(qǐng)求數(shù)據(jù)的Stream對(duì)象
            }
            catch (Exception ex)
            {
                return "";
            }

            writer.Write(bytes, 0, bytes.Length);       //把參數(shù)數(shù)據(jù)寫入請(qǐng)求數(shù)據(jù)流
            writer.Close();

            try
            {
                response = (HttpWebResponse)request.GetResponse();      //獲得響應(yīng)
            }
            catch (WebException ex)
            {
                return "";
            }

            #region 這種方式讀取到的是一個(gè)返回的結(jié)果字符串
            Stream stream = response.GetResponseStream();        //獲取響應(yīng)流
            XmlTextReader Reader = new XmlTextReader(stream);
            Reader.MoveToContent();
            result = Reader.ReadInnerXml();
            #endregion

            #region 這種方式讀取到的是一個(gè)Xml格式的字符串
            //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            //result = reader.ReadToEnd();
            #endregion 

            response.Dispose();
            response.Close();

            //reader.Close();
            //reader.Dispose();

            Reader.Dispose();
            Reader.Close();

            stream.Dispose();
            stream.Close();

            return result;
        }
    }

如果遇到調(diào)用時(shí)報(bào)錯(cuò),可以嘗試在WebService的web.config配置文件中添加如下節(jié)點(diǎn)

<system.web>
    <webServices>
      <protocols>
        <add name="HttpPost" />
      </protocols>
    </webServices>
  </system.web>

到此這篇關(guān)于C#中使用HttpPost調(diào)用WebService的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

大方县| 区。| 新巴尔虎左旗| 科技| 贵德县| 五指山市| 鄂托克旗| 南安市| 汉阴县| 抚远县| 景东| 黎平县| 荃湾区| 屯门区| 信宜市| 邻水| 宽甸| 罗平县| 景宁| 揭阳市| 封开县| 藁城市| 丰城市| 那曲县| 文水县| 阜新市| 铜山县| 梨树县| 梅河口市| 京山县| 合水县| 鄱阳县| 尤溪县| 泗阳县| 镶黄旗| 外汇| 咸阳市| 襄城县| 京山县| 晋城| 务川|