如何发布的.NET SOAP请求? [英] How to post SOAP Request from .NET?

查看:156
本文介绍了如何发布的.NET SOAP请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件中的SOAP请求。我要张贴请求在.NET Web服务 如何实现?

解决方案

VAR URI =新的URI(HTTP://本地主机/ SOAP / SOAPSMS.asmx /加); VAR REQ =(HttpWebRequest的)WebRequest.CreateDefault(URI); req.Co​​ntentType =为text / xml;字符集= UTF-8; req.Method =POST; req.Accept =为text / xml; req.Headers.Add(SOAPAction的,的http://localhost/SOAP/SOAPSMS.asmx/add); VAR strSoapMessage = @< XML版本=1.0编码=UTF-8&GT?; <肥皂:信封XMLNS:肥皂=HTTP://schemas.xmlsoap.org/soap/envelope/                的xmlns:XSI =HTTP://www.w3.org/2001/XMLSchema-instance                的xmlns:XSD =HTTP://www.w3.org/2001/XMLSchema'>   <肥皂:身体与GT;<添加的xmlns =HTTP://tempuri.org/'>< A> 23℃; / A>< B> 5℃; / B>< / SOAP:身体与GT; < /肥皂:信封>中; 使用(VAR流=新的StreamWriter(req.GetRequestStream(),Encoding.UTF8))     stream.Write(strSoapMessage);

I have the SOAP request in an XML file. I want to post the request to the web service in .net How to implement?

解决方案

var uri = new Uri("http://localhost/SOAP/SOAPSMS.asmx/add");

var req = (HttpWebRequest) WebRequest.CreateDefault(uri); 
req.ContentType = "text/xml; charset=utf-8"; 
req.Method = "POST"; 
req.Accept = "text/xml"; 
req.Headers.Add("SOAPAction", "http://localhost/SOAP/SOAPSMS.asmx/add"); 

var strSoapMessage = @"<?xml version='1.0' encoding='utf-8'?>
<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' 
               xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' 
               xmlns:xsd='http://www.w3.org/2001/XMLSchema'>
  <soap:Body><add xmlns='http://tempuri.org/'><a>23</a><b>5</b></soap:Body>
</soap:Envelope>"; 

using (var stream = new StreamWriter(req.GetRequestStream(), Encoding.UTF8)) 
    stream.Write(strSoapMessage); 

这篇关于如何发布的.NET SOAP请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆