我在XML文件中有SOAP请求。我想将请求发布到.net中的Web服务 如何实施?
答案 0 :(得分:19)
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);
答案 1 :(得分:5)
我做过类似的事情,手动构建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();
代码开头的字符串变量是你设置的,然后你从服务器得到一个字符串响应(希望......)。
答案 2 :(得分:3)
这不是正常的方式。通常,您可以使用WCF或旧式Web服务引用为您生成代理客户端。
但是,您通常需要做的是使用HttpWebRequest连接到URL,然后在请求正文中发送XML。
答案 3 :(得分:2)
我想知道XML是如何生成的,它是否是一个有效的SOAP消息?您可以按照上述人员的建议通过HTTP发布。
如果你想测试它是否有效,你可以尝试SoapUI(我的意思是测试)。
答案 4 :(得分:2)
这是另一个例子 - 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))
答案 5 :(得分:1)
很抱歉在这里碰到旧线程是我的解决方案
''' <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
答案 6 :(得分:0)
您需要通过HTTP发布数据。使用WebRequest class发布数据。您需要使用post请求发送其他数据,以确保您拥有有效的SOAP信封。有关所有详细信息,请阅读SOAP spec。