Marek
Marek

Reputation: 3575

Webservice method result issue via SOAP

Hello I have this code below in which I connected through webservice cz.mfcr.adisrws (pictured) and I need to get some of these values according to what was called in CreateSoapEnvelope() enter image description here

with this code:

 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);
        }

    }

I dont know what should be in this line:

            var _url = "http://schemas.xmlsoap.org/soap/envelope/"; //issue
        var _action = cz.mfcr.adisrws.InformaceOPlatciType(); //issue 

May someone help me solve this out?

Thanks in advance.

I receive this exception:

The remote server returned an error: (405) Method Not Allowed 

I followed this tutorial.

Upvotes: 1

Views: 401

Answers (1)

veljkoz
veljkoz

Reputation: 8512

The _url is the URL of the service - it is the URL (the 'address') where you're hosting your service - if you're hosting it yourself, it should probably be something like:

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

or if you're using the service that somebody else already hosted, then you have to see the URL they provided for it, and put that value in. The value you're currently using (http://schemas.xmlsoap.org/soap/envelope/) is just a layout of the schema for the data, not the actual URL, and esp. not the service itself (it's maybe confusing because of the http, but it's just a way of 'describing' data)

The _action part - that's the method on the service that you're trying to call, and that should also be a string, for example:

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

You have to think about what you are trying to achieve and who-does-what-and-where...

Upvotes: 1

Related Questions