之前调用 WebService 都是直接添加服务引用,然后调用 WebService 方法的,最近发现还可以使用 Http 请求调用 WebService。这里还想说一句,还是 web api 的调用简单。

    WebService 服务端代码:

    1. public class WebServiceDemo : System.Web.Services.WebService
    2. {
    3. [WebMethod]
    4. public string HelloWorld()
    5. {
    6. return "Hello World";
    7. }
    8. [WebMethod]
    9. public string Sum(string param1, string param2)
    10. {
    11. int num1 = Convert.ToInt32(param1);
    12. int num2 = Convert.ToInt32(param2);
    13. int sum = num1 + num2;
    14. return sum.ToString();
    15. }
    16. }

    很简单的代码,只是用于演示。

    客户端调用代码:

    1. class Program
    2. {
    3. static void Main(string[] args)
    4. {
    5. Program program = new Program();
    6. string url = "http://localhost:12544/WebServiceDemo.asmx";
    7. string method = "Sum";
    8. string num1 = "1";
    9. string num2 = "2";
    10. string result = program.HttpPostWebService(url, method, num1, num2);
    11. Console.WriteLine(result);
    12. Console.ReadKey();
    13. }
    14. public string HttpPostWebService(string url,string method,string num1,string num2)
    15. {
    16. string result = string.Empty;
    17. string param = string.Empty;
    18. byte[] bytes = null;
    19. Stream writer = null;
    20. HttpWebRequest request = null;
    21. HttpWebResponse response = null;
    22. param = HttpUtility.UrlEncode("param1") + "=" + HttpUtility.UrlEncode(num1) + "&" + HttpUtility.UrlEncode("param2") + "=" + HttpUtility.UrlEncode(num2);
    23. bytes = Encoding.UTF8.GetBytes(param);
    24. request = (HttpWebRequest)WebRequest.Create(url + "/" + method);
    25. request.Method = "POST";
    26. request.ContentType = "application/x-www-form-urlencoded";
    27. request.ContentLength = bytes.Length;
    28. try
    29. {
    30. writer = request.GetRequestStream(); //获取用于写入请求数据的Stream对象
    31. }
    32. catch (Exception ex)
    33. {
    34. return "";
    35. }
    36. writer.Write(bytes, 0, bytes.Length); //把参数数据写入请求数据流
    37. writer.Close();
    38. try
    39. {
    40. response = (HttpWebResponse)request.GetResponse(); //获得响应
    41. }
    42. catch (WebException ex)
    43. {
    44. return "";
    45. }
    46. #region 这种方式读取到的是一个返回的结果字符串
    47. Stream stream = response.GetResponseStream(); //获取响应流
    48. XmlTextReader Reader = new XmlTextReader(stream);
    49. Reader.MoveToContent();
    50. result = Reader.ReadInnerXml();
    51. #endregion
    52. #region 这种方式读取到的是一个Xml格式的字符串
    53. //StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
    54. //result = reader.ReadToEnd();
    55. #endregion
    56. response.Dispose();
    57. response.Close();
    58. //reader.Close();
    59. //reader.Dispose();
    60. Reader.Dispose();
    61. Reader.Close();
    62. stream.Dispose();
    63. stream.Close();
    64. return result;
    65. }
    66. }

    第一种读取方式的返回结果:

    C# 使用 HttpPost 请求调用 WebService - 图1
    第二种读取方式的返回结果:
    C# 使用 HttpPost 请求调用 WebService - 图2

    PS:如果遇到调用时报错,可以尝试在服务端(即WebService)的 web.config 配置中添加如下配置节点。

    1.  <system.web>
    2. <webServices>
    3. <protocols>
    4. <add name="HttpPost" />
    5. </protocols>
    6. </webServices>
    7. </system.web>