none
Pass Parameters to SOAP / Read the Output RRS feed

  • Question

  • Hi,

    I am trying to read the following service:

    https://www.makani.ae/MakaniPublicDataService/MakaniPublic.svc?singleWsdl

    Here is my code:

    private async void GetMakani2()
    {
        string url = "https://www.makani.ae/MakaniPublicDataService/MakaniPublic.svc?singleWsdl";
        string SOAPAction = "GetMakaniDetails";
    
        HttpClient httpClient = new HttpClient();
        HttpContent httpContent = new StringContent("");
        HttpResponseMessage response;
        // If your Service have any Authorization tokens, you have to pass those in the below format.
        // httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url);
        req.Headers.Add("SOAPAction", SOAPAction);
        req.Method = HttpMethod.Post;
        req.Content = httpContent;
        req.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml; charset=utf-8");
        // Here you will get the Reponse from service
        response = await httpClient.SendAsync(req);
        // Converting the response into text format
        var responseBodyAsText = await response.Content.ReadAsStringAsync();
    
        System.Diagnostics.Debug.WriteLine(responseBodyAsText);
    }

    I want to get the GetMakaniDetails which expect two parameters:

    1. makanino
    2. remarks

    A sample of makanino is 2487682434 and remarks can anything or blank.

    How can I pass those two parameters to the service in the above code?

    My second question is, how can I read the data from the output?

    Thanks,

    Jassim

    Friday, September 13, 2019 6:45 AM

All replies

  • Hi,

    I am trying to consume WSDL service

    I have added Connected Service to my solution but when I run it I get this error:

    System.ServiceModel.FaultException`1[[System.ServiceModel.ExceptionDetail, System.ServiceModel, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: 'Error in deserializing body of request message for operation 'GetMakaniDetails'. OperationFormatter encountered an invalid Message body. Expected to find node type 'Element' with name 'GetMakaniDetails' and namespace 'http://tempuri.org/'. Found node type 'Element' with name 'GetMakaniDetailsAsync' and namespace 'http://tempuri.org/''

    This is the service URL:

    http://www.makani.ae/MakaniPublicDataService/MakaniPublic.svc?singleWsdl

    Kindly help..

    Thanks,

    Jassim

    Thursday, September 12, 2019 5:00 PM
  • If you changed the member name for Async and want it to retain previous name, you need to specify it with appropiate attributes.

    For example, in WCF webservice it would be [DataMember(Name = "GetMakaniDetails")], and in ordinary WebService it would be [XmlElement(ElementName = "GetMakaniDetails")].


    • Edited by cheong00 Friday, September 13, 2019 1:27 AM
    Friday, September 13, 2019 1:24 AM
  • I have created another thread because I moved to code behind instead of connected service

    The thread is here:

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/39c0694f-b1ed-4882-9c87-00e49bd1026e/pass-parameters-to-soap-read-the-output?forum=netfxbcl

    Thanks/

    Friday, September 13, 2019 6:50 AM
  • You can put anything you want into a class, then serialize it and put it into StringContent in order to send it.

    And to read things back, just get the response stream content for serializer to deserialize it.

    Btw, if the remote target is proper web service, why not use "wsdl.exe" in your "NETFX 4.x Tools" to generate the proxy classes automatically, or better, in your project you add "Web Reference" of target web service to have it generate the proxy classes for you.

    Friday, September 13, 2019 7:45 AM
  • But how will I know the content format required for the SOAP? for example here the two parameters
    Friday, September 13, 2019 10:09 AM
  • Why won't you just test doing it?

    public string GetMakaniInfoFromCoord(string latitude, string longitude, string remarks);

    public string GetMakaniDetails(string makanino, string remarks);

    public string IsValidMakani(string makanino, string remarks);

    public string GetQRCodeForMakani(string makanino, string remarks);

    It will accept whatever the web service is expecting.

    Friday, September 13, 2019 2:46 PM
  • I tried below but did not work:

    private string ConstructSoapRequest(string a, string b)
    {
        return String.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>
        <s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
            <s:Body>
                <Add xmlns=""http://CalculatorService/"">
                    <a>{0}</a>
                    <b>{1}</b>
                </Add>
            </s:Body>
        </s:Envelope>", a, b);
    }
    
    private async void GetMakani()
    {
        var soapString = this.ConstructSoapRequest(EntryMakaniNumber.Text, "");
    
        string url = "https://www.makani.ae/MakaniPublicDataService/MakaniPublic.svc?singleWsdl";
    
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Add("SOAPAction", url);
    
            var content = new StringContent(soapString, Encoding.UTF8, "text/xml");
    
            using (var response = await client.PostAsync("https://www.makani.ae/MakaniPublicDataService/MakaniPublic.svc?singleWsdl", content))
            {
                var soapResponse = await response.Content.ReadAsStringAsync();
    
                System.Diagnostics.Debug.WriteLine("MAKANI RESPONSE");
                System.Diagnostics.Debug.WriteLine("---------------");
                System.Diagnostics.Debug.WriteLine(soapResponse);
                System.Diagnostics.Debug.WriteLine("---------------");
                System.Diagnostics.Debug.WriteLine("MAKANI RESPONSE");
            }
        }
    
        LabelLatitude.Text = "Latitude: ";
        LabelLongitude.Text = "Longitude: ";
    }
    

    Here is a sample project:

    https://www.jassimrahma.net/temp/XamarinMakani.zip

    Thanks,

    Jassim

    Saturday, September 14, 2019 1:58 AM
  • Hi Jassim Rahma,

    Thank you for posting here.

    I note that the project you provided is more related to Xamarin, so I suggest that you could post in Xamarin forums.

    The CLR Forum discuss and ask questions about .NET Framework Base Classes (BCL) such as Collections, I/O, Regigistry, Globalization, Reflection. Also discuss all the other Microsoft libraries that are built on or extend the .NET Framework.

    Thank you for your understanding.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 17, 2019 3:06 AM