Ok there is this webservice that I need to consume from which I know nothing about, all I know is what it's meant to do: it receives a person's national ID number and returns some information about him on xml format. I need to consume it from a C# client on an aspx page.
Since I couldn't create a web reference to the WS, I came to SO and asked this first: Unknown WebService description and consume it from C#
On that question I was suggested to rather make an HttpClient instead of a reference, so I started googling around and learned some stuff; right now I've been able to do this:
protected void rutBTN_Click(object sender, EventArgs e) //This function triggers when the user clicks the button
{ //aside the textbox where they're meant to input the ID number
if (rutTB.Text != "") //rutTB is the textbox aside the button
{
HttpClient client = new HttpClient(); // Here i'm creating an instance of an HTTP client
var byteArray = Encoding.ASCII.GetBytes("user:password123"); //Load credentials into a byte array (censored since there should be the actual credentials)
client.BaseAddress = new Uri("http://wschsol.mideplan.cl"); //Base address of the web-service
var hResp = "mod_perl/xml/fps_by_rut?rut=" + rutTB.Text; //Creating a variable that holds the rest of the WS's URL with the ID number value added to it
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); //Server authentication header with the byte array turned into a string
client.GetAsync(hResp).ContinueWith( //Here I'm sending the GET request to the WS
(requestTask) =>
{
HttpResponseMessage resp = requestTask.Result; //This task receives the WS's http response and assigns it to a HttpResponseMessage variable called resp
try
{
resp.EnsureSuccessStatusCode(); //This line is to check that the response was successful or else throw an exception
XmlDocument xmlResp = new XmlDocument(); //Creating an instance of an xml document
resp.Content.ReadAsStringAsync<XmlDocument>().ContinueWith( //OK HERE IS WHERE I'M LOST AT, trying to take the content of the http response into my xml document instance
(readTask) =>
{
/* This is where i want to parse the xml document data obtained into some grids or something. */
});
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}
});
}
else
{
testLBL.Text = "You must enter an RUT number"; // Error label
testLBL.Visible = true;
}
}
The code line on which I'm lost at is underlined by my VS2010. I know this line is wrong, I based myself on this snippet https://code.msdn.microsoft.com/introduction-to-httpclient-4a2d9cee; the guy there loads the response onto a JsonArray instead of an XML Document. So i'd be greatly thankful if you guys could tell me how to get that code line right, and if something else is wrong in my function. Anyways, the webservice's response comes through the browser on XML format, so maybe I don't even need to convert it to an xml document?? I'm pretty lost, i'm quite a noob yet at software development so i'll be hugely grateful if you guys can help me out.
Thank you guys for your kind assistance.
Try this:
Then, graduate to this:
In .NET 4.5, use "await" instead of "ContinueWith" when possible.