MaL*_*_eS 23 .net c# soap wsdl web-services
我必须连接到第三方Web服务,该服务不提供wsdl或asmx.该服务的URL只是http://server/service.soap
我已经阅读了有关原始服务调用的这篇文章,但我不确定这是否是我正在寻找的.
此外,我已经要求wsdl文件,但被告知没有(并且不会).
我正在使用带有.net 2.0的C#,并且无法升级到3.5(所以还没有WCF).我认为第三方正在使用java,因为这是他们提供的示例.
提前致谢!
更新浏览网址时获取此响应:
<SOAP-ENV:Envelope>
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Server</faultcode>
<faultstring>
Cannot find a Body tag in the enveloppe
</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Run Code Online (Sandbox Code Playgroud)
MaL*_*_eS 25
好吧,我终于让它工作了,所以我会在这里写下我正在使用的代码.(请记住,.Net 2.0,没有wsdl来自Web服务).
首先,我们创建一个HttpWebRequest:
public static HttpWebRequest CreateWebRequest(string url)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAP:Action");
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
Run Code Online (Sandbox Code Playgroud)
接下来,我们调用webservice,传递所需的所有值.当我从xml文档中读取soap信封时,我将把数据作为StringDictionary处理.应该是一个更好的方法来做到这一点,但我稍后会考虑这个:
public static XmlDocument ServiceCall(string url, int service, StringDictionary data)
{
HttpWebRequest request = CreateWebRequest(url);
XmlDocument soapEnvelopeXml = GetSoapXml(service, data);
using (Stream stream = request.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
IAsyncResult asyncResult = request.BeginGetResponse(null, null);
asyncResult.AsyncWaitHandle.WaitOne();
string soapResult;
using (WebResponse webResponse = request.EndGetResponse(asyncResult))
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
File.WriteAllText(HttpContext.Current.Server.MapPath("/servicios/" + DateTime.Now.Ticks.ToString() + "assor_r" + service.ToString() + ".xml"), soapResult);
XmlDocument resp = new XmlDocument();
resp.LoadXml(soapResult);
return resp;
}
Run Code Online (Sandbox Code Playgroud)
所以,就是这样.如果有人认为必须将GetSoapXml添加到答案中,我会将其写下来.
Joh*_*ers 15
在我看来,没有理由让SOAP Web服务不提供WSDL.它不需要由服务动态生成; 它不需要通过互联网提供.但是必须有一个WSDL,即使他们必须在软盘驱动器上发送给你!
如果您有任何能力向这项服务的提供者投诉,那么我强烈建议您这样做.如果你有能力推回,那么就这样做.理想情况下,切换服务提供商,并告诉这些人,因为他们没有提供WSDL.至少,找出他们认为不重要的原因.
小智 5
我创建了以下辅助方法来手动调用 WebService,无需任何参考:
public static HttpStatusCode CallWebService(string webWebServiceUrl,
string webServiceNamespace,
string methodName,
Dictionary<string, string> parameters,
out string responseText)
{
const string soapTemplate =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<soap:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance""
xmlns:xsd=""http://www.w3.org/2001/XMLSchema""
xmlns:soap=""http://www.w3.org/2003/05/soap-envelope"">
<soap:Body>
<{0} xmlns=""{1}"">
{2} </{0}>
</soap:Body>
</soap:Envelope>";
var req = (HttpWebRequest)WebRequest.Create(webWebServiceUrl);
req.ContentType = "application/soap+xml;";
req.Method = "POST";
string parametersText;
if (parameters != null && parameters.Count > 0)
{
var sb = new StringBuilder();
foreach (var oneParameter in parameters)
sb.AppendFormat(" <{0}>{1}</{0}>\r\n", oneParameter.Key, oneParameter.Value);
parametersText = sb.ToString();
}
else
{
parametersText = "";
}
string soapText = string.Format(soapTemplate, methodName, webServiceNamespace, parametersText);
using (Stream stm = req.GetRequestStream())
{
using (var stmw = new StreamWriter(stm))
{
stmw.Write(soapText);
}
}
var responseHttpStatusCode = HttpStatusCode.Unused;
responseText = null;
using (var response = (HttpWebResponse)req.GetResponse())
{
responseHttpStatusCode = response.StatusCode;
if (responseHttpStatusCode == HttpStatusCode.OK)
{
int contentLength = (int)response.ContentLength;
if (contentLength > 0)
{
int readBytes = 0;
int bytesToRead = contentLength;
byte[] resultBytes = new byte[contentLength];
using (var responseStream = response.GetResponseStream())
{
while (bytesToRead > 0)
{
// Read may return anything from 0 to 10.
int actualBytesRead = responseStream.Read(resultBytes, readBytes, bytesToRead);
// The end of the file is reached.
if (actualBytesRead == 0)
break;
readBytes += actualBytesRead;
bytesToRead -= actualBytesRead;
}
responseText = Encoding.UTF8.GetString(resultBytes);
//responseText = Encoding.ASCII.GetString(resultBytes);
}
}
}
}
// standard responseText:
//<?xml version="1.0" encoding="utf-8"?>
//<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
// <soap:Body>
// <SayHelloResponse xmlns="http://tempuri.org/">
// <SayHelloResult>Hello</SayHelloResult>
// </SayHellorResponse>
// </soap:Body>
//</soap:Envelope>
if (!string.IsNullOrEmpty(responseText))
{
string responseElement = methodName + "Result>";
int pos1 = responseText.IndexOf(responseElement);
if (pos1 >= 0)
{
pos1 += responseElement.Length;
int pos2 = responseText.IndexOf("</", pos1);
if (pos2 > pos1)
responseText = responseText.Substring(pos1, pos2 - pos1);
}
else
{
responseText = ""; // No result
}
}
return responseHttpStatusCode;
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用以下代码简单地调用任何 Web 服务方法:
var parameters = new Dictionary<string, string>();
parameters.Add("name", "My Name Here");
string responseText;
var responseStatusCode = CallWebService("http://localhost/TestWebService.asmx",
"http://tempuri.org/",
"SayHello",
parameters,
out responseText);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
44438 次 |
| 最近记录: |