-1

I need to call this API freegeoip.net/xml/userIpAddress during my controller to get its data. like this freegeoip.net/xml/4.2.2.2

here is my controller

public ActionResult Index(string language)
{
    if (String.IsNullOrWhiteSpace(language) == false)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
    }

     else if (String.IsNullOrWhiteSpace(language))
    {
        string userIpAddress = this.Request.UserHostAddress;

       //here how I can call freegeoip.net/xml/userIpAddress

    }

}

here is what an XML response looks:

<Response>
 <IP>4.2.2.2</IP>
 <CountryCode>US</CountryCode>
 <CountryName>United States</CountryName>
 <RegionCode/>
 <RegionName/>
 <City/>
 <ZipCode/>
 <TimeZone/>
 <Latitude>37.751</Latitude>
 <Longitude>-97.822</Longitude>
 <MetroCode>0</MetroCode>
 </Response>
2
  • You can use HttpClient to make a request to the constructed url which would include the ip address. I would advise wrapping/extracting that into a service. Commented Jun 12, 2017 at 11:22
  • @Nkosi thank you so much. is it possible to answer my question and give me some more details? please. thank you very much Commented Jun 12, 2017 at 12:10

1 Answer 1

2

Use HttpClient:

public async Task<ActionResult> Index(string language)
{
    if (String.IsNullOrWhiteSpace(language) == false)
    {
        Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(language);
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
    }

    else if (String.IsNullOrWhiteSpace(language))
    {
        string userIpAddress = this.Request.UserHostAddress;

        var client = new HttpClient
        {
            BaseAddress = new Uri("http://freegeoip.net/xml/")
        };

        var response = await client.GetAsync(userIpAddress);

        var content = await response.Content.ReadAsStringAsync();

        var result = (Response)new XmlSerializer(typeof(Response)).Deserialize(new StringReader(content));

        // do stuff
    }

    ...
}

[XmlRoot(ElementName = "Response")]
public class Response
{
    [XmlElement(ElementName = "IP")]
    public string IP { get; set; }
    [XmlElement(ElementName = "CountryCode")]
    public string CountryCode { get; set; }
    [XmlElement(ElementName = "CountryName")]
    public string CountryName { get; set; }
    [XmlElement(ElementName = "RegionCode")]
    public string RegionCode { get; set; }
    [XmlElement(ElementName = "RegionName")]
    public string RegionName { get; set; }
    [XmlElement(ElementName = "City")]
    public string City { get; set; }
    [XmlElement(ElementName = "ZipCode")]
    public string ZipCode { get; set; }
    [XmlElement(ElementName = "TimeZone")]
    public string TimeZone { get; set; }
    [XmlElement(ElementName = "Latitude")]
    public string Latitude { get; set; }
    [XmlElement(ElementName = "Longitude")]
    public string Longitude { get; set; }
    [XmlElement(ElementName = "MetroCode")]
    public string MetroCode { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Do you mean how to deserialize the response? If so: stackoverflow.com/questions/19942486/…
thank you very much. helpful. I'm really new to API's. is it possible to help me step by step to make it complete? now I should create a class ?
Added a deserialization example. I used this site to generate the object: xmltocsharp.azurewebsites.net

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.