1

I am calling an external service using GetAsync() and passing parameters in query string. When i check the content in the response, i don't see anything returned, however it returns 200 OK and in fiddler it returns me the XML response correctly. I need the XML response to get de-serialize to an C# object and then further save it to DB.

Things tried:

1) Tried this by adding this setting in global- app_start(), It didn't help

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

2) Created an object and tried to sent it via GetAysnc, that didn't help either.

public class Request
    {
        [XmlElement]
        public string XML { get; set; }
        [XmlElement]
        public List<string> ProNumber { get; set; }
    }

2) Should i try passing parameters in query string and expect json result? if i add mediatyperformatter to application/json?

Here is my code:

public async Task<HttpResponseMessage> GetData()
{
    string requestString = "&xml=Y&PRONumber=82040X,03117X";
    string result = "";

    string url = @"http://my.yrc.com/dynamic/national/servlet?CONTROLLER=com.rdwy.ec.rextracking.http.controller.PublicTrailerHistoryAPIController";
    try
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(url);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

            HttpResponseMessage response = await client.GetAsync(url+requestString);

            if (response.IsSuccessStatusCode)
            {
                return response;
            }
        }
    }
    catch (Exception ex)
    {
        result = ex.Message;
    }
    return null;
}

EDIT:

                        Shipments scp = null;
                        XmlRootAttribute xRoot = new XmlRootAttribute();
                        xRoot.ElementName = "Shipment";
                        xRoot.IsNullable = true;
                        XmlSerializer serializer = new XmlSerializer(typeof(Shipment), xRoot);

                    using (Stream stream = response.Content.ReadAsStreamAsync().Result)
                    {
                        scp = (Shipments)serializer.Deserialize(stream);
                    }

Model:

public class Shipments
    {
        [XmlArrayItem(Type = typeof(Shipment))]
        public Shipment[] Shipment;
    }

    public class Shipment
    {
        [XmlAttribute()]
        public int returnCode { get; set; }
        .................
        ..............

Getting error:<SHIPMENTS xmlns=''> was not expected.

Any help on this is much appreciated.

Thanks, WH

3
  • You probably are getting the data in multiple packets and the 200 response is the last packet. Verify in fiddler that the data is in a previous packet. If you are still having issues use an IE and manually go to the URL and capture results with fiddler and compare to your C# application. Commented Jan 24, 2016 at 17:11
  • 1
    Works fine for me. What are you doing with response.Content? You need to call ReadAsStreamAsync or one of the other methods, Commented Jan 24, 2016 at 19:59
  • @CharlesMager- Tried but getting error Errors de-serializing XML: {“<query xmlns=''> was not expected.”} Commented Jan 25, 2016 at 14:09

1 Answer 1

1

This worked for me -

var client = new HttpClient();
var data = client.GetStringAsync("http://my.yrc.com/dynamic/national/servlet?CONTROLLER=com.rdwy.ec.rextracking.http.controller.PublicTrailerHistoryAPIController&xml=Y&PRONumber=82040X,03117X").Result;
var ser = new XmlSerializer(typeof(Shipments));
var t = (Shipments)ser.Deserialize(new StringReader(data));


public class Shipment
{
    public string returnCode { get; set; }
    public string returnMessage { get; set; }
    public string freightBillNumber { get; set; }
    //props
}

[XmlRoot(ElementName = "SHIPMENTS")]
public class Shipments
{
    [XmlElement(ElementName = "SHIPMENT")]
    public List<Shipment> SHIPMENT { get; set; }
}

EDIT

this works as well -

var data = client.GetStreamAsync("http://my.yrc.com/dynamic/national/servlet?CONTROLLER=com.rdwy.ec.rextracking.http.controller.PublicTrailerHistoryAPIController&xml=Y&PRONumber=82040X,03117X").Result;

EDIT

works as well -

var client = new HttpClient();
var data = client.GetAsync("http://my.yrc.com/dynamic/national/servlet?CONTROLLER=com.rdwy.ec.rextracking.http.controller.PublicTrailerHistoryAPIController&xml=Y&PRONumber=82040X,03117X").Result;

var ser = new XmlSerializer(typeof(Shipments));
var t = (Shipments)ser.Deserialize(data.Content.ReadAsStreamAsync().Result);
Sign up to request clarification or add additional context in comments.

1 Comment

It works , but why we should we get the response(xml here) as a string instead of using ReadAsStreamAsync() which reads the response, one line at a time

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.