2

I would like to get a method that generates an http post with the following configuration in c#.

  POST pqs.php HTTP/1.1
  Host: nationalmap.gov/epqs/pqs.php
  Content-Type: application/x-www-form-urlencoded
  Content-Length: length
  x=string&y=string&units=string&output=string

I tried the following however I am getting invalid coordinates although they do exist in the map ( example tried is lat = 36.574832 and lon = -85.411825

Here is the code I am using:

public class ElevationUSGISPull
{
    public string responseString = null;
    private string BASEURL = "http://nationalmap.gov/epqs/pqs.php";

    public bool httpPostElevationRequest( string lon,string lat)
    {

        try
        {
            using (WebClient client = new WebClient())
            {

                byte[] response =
                client.UploadValues(BASEURL, new NameValueCollection()
                {
                { "x", lon },
                { "y", lat },
                {"units", "feet" },
                {"output","json" },
                });

                string result = System.Text.Encoding.UTF8.GetString(response);
                responseString = result;
            }
            return true;
        }
        catch(Exception eX)
        {
            return false;
        }
    }
}

The error I am getting is as below:

<error>General failure: Invalid Coordinates</error>

Moreover has anyone tried the .gov website to get elevation data. I used the google server however it has many restrictions related to number of requests and I am trying to do many requests for a given area.

Below is a website for verifying the lat and longs with a good json response.

https://nationalmap.gov/epqs/

Thanks all.

I have also tried the following example here: http://ronaldrosiernet.azurewebsites.net/Blog/2013/12/07/posting_urlencoded_key_values_with_httpclient

But is giving me the following error.

Exception thrown: 'System.NullReferenceException'

I modified the code to this:

  public async Task<string> HTTPPOST(string lon, string lat)
    {
        var client = new HttpClient();
        client.BaseAddress = new Uri(BASEURL);
        var request = new HttpRequestMessage(HttpMethod.Post, "");

        var keyValues = new List<KeyValuePair<string, string>>();
        keyValues.Add(new KeyValuePair<string, string>("x", lon));
        keyValues.Add(new KeyValuePair<string, string>("y", lat));
        keyValues.Add(new KeyValuePair<string, string>("units", "feet"));
        keyValues.Add(new KeyValuePair<string, string>("output", "json"));

        request.Content = new FormUrlEncodedContent(keyValues);
        var response = await client.SendAsync(request);

        return response.ToString();
    }
nader
  • 141
  • 2
  • 13

2 Answers2

1

After debugging for an hour I noticed that I had units as a parameter rather than unit :( ......

    public string httpPostElevationRequest( string lon,string lat)
    {
        try
        {
            using (WebClient client = new WebClient())
            {

                byte[] response =
                client.UploadValues(BASEURL, new NameValueCollection()
                {
                { "x", lon },
                { "y", lat },
                {"unit", "feet" },
                {"output","json" },
                });
                string result = System.Text.Encoding.UTF8.GetString(response);
                responseString = result;
            }
            return responseString;
        }
        catch(Exception eX)
        {
            return null;
        }
    }

Also baseurl is as follows:

private string BASEURL = "https://nationalmap.gov/epqs/pqs.php";
nader
  • 141
  • 2
  • 13
  • ***HttpClient*** vs ***WebClient? – Kiquenet Mar 05 '18 at 09:28
  • Here is comparison of the two: http://www.diogonunes.com/blog/webclient-vs-httpclient-vs-httpwebrequest/. Seems like httpclient is compatible with newer frameworks. – nader Jun 12 '19 at 22:04
0

Use .NET HttpClient Find an example here

Community
  • 1
  • 1
McKabue
  • 2,076
  • 1
  • 19
  • 34