1

您好,我在下面有这段代码,我通过它连接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 

我按照教程进行操作。

4

1 回答 1

1

_url是服务的 URL - 它是您托管服务的 URL(“地址”) - 如果您自己托管它,它可能应该是这样的:

_url = "http://localhost/MyService/MyService.asmx"

或者,如果您正在使用其他人已经托管的服务,那么您必须查看他们为其提供的 URL,然后输入该值。您当前使用的值 ( http://schemas.xmlsoap.org/soap/envelope/) 只是架构的布局数据,而不是实际的 URL,尤其是。不是服务本身(它可能因为http而令人困惑,但这只是“描述”数据的一种方式)

部分 -这_action是您尝试调用的服务上的方法,它也应该是一个字符串,例如:

_action = "http://localhost/MyService/MyService.asmx?op=HelloWorld"

您必须考虑您要实现的目标以及谁在做什么以及在哪里...

于 2013-08-27T17:08:55.757 回答