C # Use HttpPost to call WebService,

Source: Internet
Author: User

C # Use HttpPost to call WebService,

Previously, WebService was called by directly adding service references and then calling the WebService method. Recently, we found that WebService can be called using Http requests. Here I would like to say that the web api call is simple.

WebService server code:

   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();        }    }

Simple code, just for demonstration.

 

Client call code:

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 (); // get the Stream object used to write request data} catch (Exception ex) {return "";} writer. write (bytes, 0, bytes. length); // write the parameter data to the request data stream writer. close (); try {response = (HttpWebResponse) request. getResponse (); // get the response} catch (WebException ex) {return "" ;}# region reads a returned result string Stream stream = response. getResponseStream (); // get the response stream XmlTextReader Reader = new XmlTextReader (stream); Reader. moveToContent (); result = Reader. readInnerXml (); # endregion # This method reads a string in Xml format // 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 ;}}

The first read method returns the result:

The second read method returns the result:

 

PS: if an error is reported during the call, you can add the following configuration nodes to the web. config configuration of the server (WebService.

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

 

Refer:

Http://www.cnblogs.com/caiwenz/p/3910566.html

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.