Buenas tardes,
Extrañaba escribir en el foro, pero tengo un problema que quizá puedan ayudarme. Resulta que tengo una aplicación en PHP para hacer consultas a una API, pero estoy intentando traducirlo al lenguaje C#, pero sin éxito.
Código PHP:
$datos= new SoapClient($url, $conf);
$parametros=array();
$parametros['StringXML']= $StringXML;
// En PHP, tan solo envio los parametros a $datos y me funciona
$result = $datos->CrearTicket($parametros);
//Luego consulto la respuesta y puedo trabajar.
echo 'Respuesta en formato xml: ' . $result->CrearTicketResult . '<br>';
Pero en C#, intento con HttpClient, y no logro hacerlo funcionar.
Código C#:
Ver original// Archivo ConsultaController
[HttpPost]
public ActionResult Consulta()
{
Global global = new Global();
global.RunAsync().Wait();
return Content("Ok");
}
// Archivo Global.cs
static async Task<Uri> EnviarDatosXML(string xml)
{
HttpResponseMessage response = await client.PostAsXmlAsync("CrearTicket", xml);
response.EnsureSuccessStatusCode();
// return URI of the created resource.
return response.Headers.Location;
}
static async Task<XmlDocument> ObtenerRespuestXML(string path)
{
XmlDocument product = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<XmlDocument>();
}
return product;
}
public async Task RunAsync()
{
client.BaseAddress = new Uri(URL_API);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
string xmlDoc
= GeneralStringXML
("99999", "1000.00", "DANI", "O", "LIMA", "AV LIMA", "[email protected]", ""); XmlDocument ResXmlDoc = null;
var url = await EnviarDatos(xmlDoc);
Debug.WriteLine(url);
// Get the product
ResXmlDoc = await ObtenerRespuestXML(url.PathAndQuery);
//XmlNodeList xrw = ResXmlDoc.GetElementsByTagName("DNI");
//XmlNodeList xLista = ((XmlElement)xrw [0]).GetElementsByTagName("CARGO");
foreach (XmlElement nodo in xLista)
{
string xEdad = nodo.GetAttribute("EDAD");
string xNombre = nodo.InnerText;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}