consume a web service with Rest API with xml input parameters

1

I have to consume a service REST with C# , but the truth is that I'm a bit lost on this issue.

on the one hand, the service to be consumed, requires several XML parameters that have the following format

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<prescription>
<patient>
<allergies>
<allergy>vidal://allergy/1</allergy>
<allergy>vidal://allergy/2</allergy>
<allergy>vidal://allergy/3</allergy>
</allergies>
<molecules>
<molecule>vidal://molecule/1</molecule>
</molecules>
<pathologies>
<pathology></pathology>
</pathologies>
<breastFeeding>NONE</breastFeeding>
<creatin>120</creatin>
<dateOfBirth>2012-11-08T15: 44:50.980+01:00</dateOfBirth>
<gender>MALE</gender>
<height>180.0</height>
<hepaticInsufficiency>SEVERE</hepaticInsufficiency>
<weeksOfAmenorrhea>5</weeksOfAmenorrhea>
<weight>80.0</weight>
</patient>
<prescription-lines>
<prescription-line>
<drugId>1</drugId>
<drugType>COMMON_NAME_GROUP</drugType>
</prescription-line>
<prescription-line>
<drugId>2</drugId>
<drugType>COMMON_NAME_GROUP</drugType>
</prescription-line>
</prescription-lines>
</prescription>

Where the necessary data, I get it from a form.

To make the call to the service, I have a function, where I specify the base address, which incorporates the necessary credentials to use the service, in addition to the method POST for the query.

Resulting in this way

http://api-cl.vidal.fr/rest/api/alerts?app_id=5e151413&
app_key=f80c77cbd1ef392f541ee83889d1d2d4 method='post' result=" + xmlFile;

The input parameters described above, I transform them to xml , through a function that adds the tags necessary for each data.

This is the function I have to make a call.

        public string EjecutarConsulta2(string url)
    {
        string strResponseValue = string.Empty;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = httpMethod.ToString();
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new ApplicationException("Codigo Error : " + response.StatusCode);
            }

            using (Stream responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        strResponseValue = reader.ReadToEnd();
                    }
                }
            }
        }
        return strResponseValue;

    }

My problem is that I do not know where the input parameters go in XML format.

Do they have to be concatenated next to the URL? (I did this, but it did not work) .

Does anyone know how to execute these queries? Greetings

    
asked by Luis Gabriel Fabres 22.07.2017 в 03:06
source

1 answer

1

To my way of seeing if you have to send an XML, it must be sent in the Body of the request.

public string EjecutarConsulta2(string url, string xmlToSend)
{
    string strResponseValue = string.Empty;
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    request.Method = httpMethod.ToString();
    request.ContentType = "application/xml"; // establecer el ContentType de la petición a XML
    byte[] bytes = Encoding.UTF8.GetBytes(xmlToSend);

    using(Stream requestStream = request.GetRequestStream()) // generar la petición mandando al Body el XML a enviar
    {
        request.GetRequestStream().Write(bytes, 0, bytes.Length); 

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new ApplicationException("Codigo Error : " + response.StatusCode);
            }

            using (Stream responseStream = response.GetResponseStream())
            {
                if (responseStream != null)
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        strResponseValue = reader.ReadToEnd();
                    }
                }
            }
        }
    }


    return strResponseValue;
}
    
answered by 23.07.2017 в 19:20