C#使用HttpPost请求调用WebService的方法

作者:Brambling 时间:2022-02-24 07:22:16 

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

WebService 服务端代码:


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

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

客户端调用代码:


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();  //获取用于写入请求数据的Stream对象
  }
  catch (Exception ex)
  {
   return "";
  }

writer.Write(bytes, 0, bytes.Length);  //把参数数据写入请求数据流
  writer.Close();

try
  {
   response = (HttpWebResponse)request.GetResponse();  //获得响应
  }
  catch (WebException ex)
  {
   return "";
  }

#region 这种方式读取到的是一个返回的结果字符串
  Stream stream = response.GetResponseStream();  //获取响应流
  XmlTextReader Reader = new XmlTextReader(stream);
  Reader.MoveToContent();
  result = Reader.ReadInnerXml();
  #endregion

#region 这种方式读取到的是一个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;
 }
}

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

C#使用HttpPost请求调用WebService的方法

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

C#使用HttpPost请求调用WebService的方法

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


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

参考:C#使用Http Post方式传递Json数据字符串调用Web Service

标签:C#,HttpPost,WebService
0
投稿

猜你喜欢

  • java IO流将一个文件拆分为多个子文件代码示例

    2023-08-30 12:46:15
  • 使用Spring Boot进行单元测试详情

    2023-11-10 08:01:53
  • java使用influxDB数据库的详细代码

    2023-03-04 05:22:27
  • Java操作IO对象流进行数据的读写

    2021-08-25 03:33:49
  • spring框架集成flyway项目的详细过程

    2023-09-14 11:47:14
  • Java读取Map的两种方法与对比

    2021-08-08 20:56:55
  • idea在用Mybatis时xml文件sql不提示解决办法(提示后背景颜色去除)

    2023-11-09 01:45:51
  • 学习Java的9张思维导图

    2021-06-10 03:38:18
  • sharding-jdbc5.0.0实现分表实践

    2023-12-07 10:12:26
  • SpringMVC @RequestBody自动转json Http415错误的解决

    2022-09-12 13:12:34
  • 最详细的文件上传下载实例详解(推荐)

    2021-12-12 08:18:13
  • Java编程实现帕斯卡三角形代码示例

    2023-11-02 08:08:24
  • 使用@TransactionalEventListener监听事务教程

    2023-10-05 02:50:44
  • Java的动态分派和静态分派的实现

    2023-10-09 12:58:37
  • VsCode使用EmmyLua插件调试Unity工程Lua代码的详细步骤

    2022-12-25 14:13:25
  • springboot基于Mybatis mysql实现读写分离

    2023-11-27 21:42:59
  • C#中使用快速排序按文件创建时间将文件排序的源码

    2023-06-04 08:19:19
  • java 教你如何给你的头像添加一个好看的国旗

    2021-11-11 02:53:25
  • 一文快速掌握Spring Cloud Stream

    2023-09-01 23:09:38
  • SpringCloud整合Nacos实现流程详解

    2021-07-04 11:11:09
  • asp之家 软件编程 m.aspxhome.com