1

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.

Community
  • 1
  • 1
Marek
  • 3,555
  • 17
  • 74
  • 123

1 Answers1

1

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...

veljkoz
  • 8,384
  • 8
  • 55
  • 91
  • Hello I updated the server may I please ask what should be in _action? I'm not sure how to create it. I will be so thankful to you. Please. EDIT: I think I got it, May I ask how can I display the result? – Marek Aug 27 '13 at 17:28
  • I'm receiving null on sopaResult, could you please help me ? – Marek Aug 27 '13 at 17:37
  • Having null can be any number of things - you'll have to go step-by-step in debugging and see if everything is the way you expect it to be (make an excercise - "I expect this to be..." for every variable you're using)... don't get me wrong, but I think you don't understand what you're doing here, and once you do, you'll be able to see the error... It's hard for us to 'debug' someone else code just by looking at it. I'd like to help more but I can't... – veljkoz Aug 28 '13 at 07:01
  • You are totaly right, I'm not sure what I'm doing. I have never experienced this before. Cause I received the web service url and an example of calling (in xml). I thank you so much even for this, you are an awesome person. Thanks again. – Marek Aug 28 '13 at 07:11