当前位置: 移动技术网 > IT编程>开发语言>c# > C# Soap调用WebService的实例

C# Soap调用WebService的实例

2019年07月18日  | 移动技术网IT编程  | 我要评论

实例如下所示:

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的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网