Mario Guadanhim Mario Guadanhim - 4 months ago 52
HTTP Question

How get Web API IHttpActionResult response and serialize c#

I'm trying to get "response, serialize and consume" the web api, I never code a web api before.

I have the following Web API:

public IHttpActionResult SendMsg(string yourName)

var result = "Hi " + yourName;
if (result == null)
return NotFound();
return Ok(result);

and i'm trying to get the response with this code:

private void ResponseWebAPI(string strApiAddress)
HttpWebRequest request = WebRequest.Create(strApiAddress) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

XmlDocument xmlDoc = new XmlDocument();


I'm receiving the response without any problem, but when I load the response I got the following error:

Data at the root level is invalid. Line 1, position 1


Try this

private void ResponseWebAPI(string strApiAddress) {
    var request = WebRequest.Create(strApiAddress) as HttpWebRequest;
    var response = request.GetResponse() as HttpWebResponse;

    using (Stream stream = response.GetResponseStream()) {
        var reader = new StreamReader(stream, Encoding.UTF8);
        var responseString = reader.ReadToEnd();
        var xmlDoc = new XmlDocument();