C# Soap调用WebService的实例

时间:2021-11-21 13:39:48

实例如下所示:

?
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
public class WebServiceHelper
 {
  /// <summary>
  /// Soap协议Post方法
  /// </summary>
  /// <param name="uri">WebService地址</param>
  /// <param name="str">传入Soap协议格式数据</param>
  /// <returns></returns>
  public static string SendToReturn(Uri _uri, string _str)
  {
   string _returnstr = "";
   //发起请求
   WebRequest webRequest = WebRequest.Create(_uri);
   webRequest.ContentType = "text/xml; charset=utf-8";
   webRequest.Method = "POST";
   using (Stream requestStream = webRequest.GetRequestStream())
   {
    byte[] paramBytes = Encoding.UTF8.GetBytes(_str);
    requestStream.Write(paramBytes, 0, paramBytes.Length);
   }
   //响应
   try
   {
    WebResponse webResponse = webRequest.GetResponse();
    using (StreamReader myStreamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
    {
     _returnstr = myStreamReader.ReadToEnd();
    }
   }
   catch (WebException ex)
   {
    _returnstr = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
   }
   return _returnstr;
  }
 }

以上这篇C# Soap调用WebService的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/jxwforever/archive/2017/12/12/8027599.html