您好,我在下面的代码中通过webservice cz.mfcr.adisrws
连接了(如图),我需要根据CreateSoapEnvelope()
使用此代码:
namespace spolehlivost_platce
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CallWebService();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public static XmlDocument CreateSoapEnvelope()
{
XmlDocument soapEnvelop = new XmlDocument();
soapEnvelop.LoadXml
(@"<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/""><soapenv:Body><StatusNespolehlivyPlatceRequest xmlns=""http://adis.mfcr.cz/rozhraniCRPDPH/""><dic>28156609</dic></StatusNespolehlivyPlatceRequest></soapenv:Body></soapenv:Envelope>");
return soapEnvelop;
}
protected virtual WebRequest CreateRequest(ISoapMessage soapMessage)
{
var wr = WebRequest.Create(soapMessage.Uri);
wr.ContentType = "text/xml;charset=utf-8";
wr.ContentLength = soapMessage.ContentXml.Length;
wr.Headers.Add("SOAPAction", soapMessage.SoapAction);
wr.Credentials = soapMessage.Credentials;
wr.Method = "POST";
wr.GetRequestStream().Write(Encoding.UTF8.GetBytes(soapMessage.ContentXml), 0, soapMessage.ContentXml.Length);
return wr;
}
private static HttpWebRequest CreateWebRequest(string url, string action)
{
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Headers.Add("SOAPAction", action);
webRequest.ContentType = "text/xml;charset=\"utf-8\"";
webRequest.Accept = "text/xml";
webRequest.Method = "POST";
return webRequest;
}
private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
public static void CallWebService()
{
var _url = "http://schemas.xmlsoap.org/soap/envelope/"; //issue
var _action = cz.mfcr.adisrws.InformaceOPlatciType(); //issue
XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
HttpWebRequest webRequest = CreateWebRequest(_url,_action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
// begin async call to web request.
IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);
// suspend this thread until call is complete. You might want to
// do something usefull here like update your UI.
asyncResult.AsyncWaitHandle.WaitOne();
// get the response from the completed web request.
string soapResult;
using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.WriteLine(soapResult);
}
}
我不知道这一行应该是什么:
var _url = "http://schemas.xmlsoap.org/soap/envelope/"; //issue
var _action = cz.mfcr.adisrws.InformaceOPlatciType(); //issue
有人可以帮我解决这个问题吗?
提前致谢。
我收到此例外:
The remote server returned an error: (405) Method Not Allowed
我遵循了this教程。
答案 0 :(得分:1)
_url
是服务的网址 - 它是您托管服务的网址(“地址”) - 如果您自己托管服务,则应该是这样的:
_url = "http://localhost/MyService/MyService.asmx"
或者如果您正在使用其他人已经托管的服务,那么您必须查看他们为其提供的网址,并将该值放入。您当前使用的值(http://schemas.xmlsoap.org/soap/envelope/
)是只是数据架构的布局,不实际的URL,尤其是。不是服务本身(因为http可能令人困惑,但它只是一种'描述'数据的方式)
_action
部分 - 这是您尝试调用的服务上的方法,也应该是字符串,例如:
_action = "http://localhost/MyService/MyService.asmx?op=HelloWorld"
你必须考虑你想要达到的目标以及做什么和做什么......