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

时间:2022-04-11 04:35:58

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

webservice 服务端代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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();
  }
 }

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

客户端调用代码:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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 配置中添加如下配置节点。

?
1
2
3
4
5
6
7
<system.web>
 <webservices>
  <protocols>
  <add name="httppost" />
  </protocols>
 </webservices>
 </system.web>

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。