-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>
neda Derakhshesh
  • 1,103
  • 2
  • 20
  • 43
  • 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. – Nkosi Jun 12 '17 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 – neda Derakhshesh Jun 12 '17 at 12:10

1 Answers1

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; }
}
peco
  • 3,890
  • 1
  • 20
  • 27
  • 1
    Do you mean how to deserialize the response? If so: https://stackoverflow.com/questions/19942486/how-to-use-httpclient-to-read-an-xml-response – peco Jun 12 '17 at 12:21
  • 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 ? – neda Derakhshesh Jun 12 '17 at 12:31
  • 1
    Added a deserialization example. I used this site to generate the object: http://xmltocsharp.azurewebsites.net/ – peco Jun 12 '17 at 13:02