欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# Soap调用WebService的实例

程序员文章站 2023-12-16 22:46:52
实例如下所示: public class webservicehelper { /// /// soap协议pos...

实例如下所示:

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

上一篇:

下一篇: