C# uses HttpPost request to call WebService

Original: C# uses HttpPost request to call WebService

Before calling the WebService, the service reference was added directly, and then the method of the WebService was called. Recently, it has been found that the WebService can also be called using Http requests. I also want to say here that the call of web api 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();
        }
    }

Very simple code, just for demonstration.

 

Client calling 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 the request data 
            }
             catch (Exception ex)
            {
                return "";
            }

            writer.Write(bytes, 0 , bytes.Length);        // Write 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 in this way 
            Stream stream = response.GetResponseStream();         // Get the response stream 
            XmlTextReader Reader = new XmlTextReader(stream);
            Reader.MoveToContent();
            result = Reader.ReadInnerXml();
            #endregion

            #region 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 return result of the first reading method:

The return result of the second reading method:

 

PS: If you encounter an error when calling, you can try to add the following configuration node to the web.config configuration of the server (ie WebService).

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

 

refer to:

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

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325291528&siteId=291194637