如何从 .NET 发布 SOAP 请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/287126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to post SOAP Request from .NET?
提问by
I have the SOAP request in an XML file. I want to post the request to the web service in .net How to implement?
我在 XML 文件中有 SOAP 请求。我想将请求发布到 .net 中的 web 服务如何实现?
回答by
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);
回答by marcus.greasly
I've done something like this, building an xml request manually and then using the webrequest object to submit the request:
我做过这样的事情,手动构建一个 xml 请求,然后使用 webrequest 对象提交请求:
string data = "the xml document to submit";
string url = "the webservice url";
string response = "the response from the server";
// build request objects to pass the data/xml to the server
byte[] buffer = Encoding.ASCII.GetBytes(data);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = buffer.Length;
Stream post = request.GetRequestStream();
// post data and close connection
post.Write(buffer, 0, buffer.Length);
post.Close();
// build response object
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
Stream responsedata = response.GetResponseStream();
StreamReader responsereader = new StreamReader(responsedata);
response = responsereader.ReadToEnd();
The string variables at the start of the code are what you set, then you get a string response (hopefully...) from the server.
代码开头的字符串变量是您设置的,然后您会从服务器获得字符串响应(希望...)。
回答by Lou Franco
This isn't the normal way. Usually you would use WCF or the older style web service reference to generate a proxy client for you.
这不是正常的方式。通常您会使用 WCF 或旧样式的 Web 服务引用来为您生成代理客户端。
However, what you need to do generally is use HttpWebRequest to connect to the URL and then send the XML in the body of the request.
但是,您通常需要做的是使用 HttpWebRequest 连接到 URL,然后在请求正文中发送 XML。
回答by yclian
I'm wondering how's the XML generated and is it a valid SOAP message? You can post it via HTTP as suggested by the folks above.
我想知道 XML 是如何生成的,它是否是有效的 SOAP 消息?您可以按照上述人员的建议通过 HTTP 发布它。
If you want to test if that's going to work, you can give SoapUIa try (for testing I mean).
如果你想测试它是否会起作用,你可以试试SoapUI(我的意思是测试)。
回答by jeffspost
Here's another example--this one in VB:
这是另一个例子——这个在 VB 中的例子:
Dim manualWebClient As New System.Net.WebClient()
manualWebClient.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8")
' Note: don't put the <?xml... tag in--otherwise it will blow up with a 500 internal error message!
Dim bytArguments As Byte() = System.Text.Encoding.ASCII.GetBytes( _
"<soap12:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope"">" & System.Environment.NewLine & _
" <soap12:Body>" & System.Environment.NewLine & _
" <Multiply xmlns=""http://cptr446.class/"">" & System.Environment.NewLine & _
" <x>5</x>" & System.Environment.NewLine & _
" <y>4</y>" & System.Environment.NewLine & _
" </Multiply>" & System.Environment.NewLine & _
" </soap12:Body>" & System.Environment.NewLine & _
"</soap12:Envelope>")
Dim bytRetData As Byte() = manualWebClient.UploadData("http://localhost/CPTR446.asmx", "POST", bytArguments)
MessageBox.Show(System.Text.Encoding.ASCII.GetString(bytRetData))
回答by bgs264
Sorry for bumping an old thread here's my solution to this
很抱歉碰到旧线程,这是我的解决方案
''' <summary>
''' Sends SOAP to a web service and sends back the XML it got back.
''' </summary>
Public Class SoapDispenser
Public Shared Function CallWebService(ByVal WebserviceURL As String, ByVal SOAP As String) As XmlDocument
Using wc As New WebClient()
Dim retXMLDoc As New XmlDocument()
wc.Headers.Add("Content-Type", "application/soap+xml; charset=utf-8")
retXMLDoc.LoadXml(wc.UploadString(WebserviceURL, SOAP))
Return retXMLDoc
End Using
End Function
End Class
回答by Brian Lyttle
You need to post the data over HTTP. Use the WebRequest classto post the data. You will need to send other data with the post request to ensure you have a valid SOAP envelope. Read the SOAP specfor all of the details.
您需要通过 HTTP 发布数据。使用WebRequest 类发布数据。您将需要随 post 请求发送其他数据,以确保您拥有有效的 SOAP 信封。阅读SOAP 规范以了解所有详细信息。

