【问题标题】:Http Post C# with Json response带有 Json 响应的 Http Post C#
【发布时间】:2017-04-01 18:35:36
【问题描述】:

我想得到一个在c#中生成具有以下配置的http post的方法。

  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

我尝试了以下方法,但我得到了无效的坐标,尽管它们确实存在于地图中(尝试的示例是 lat = 36.574832 和 lon = -85.411825

这是我正在使用的代码:

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;
        }
    }
}

我得到的错误如下:

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

此外,有没有人尝试过 .gov 网站来获取海拔数据。我使用了谷歌服务器,但是它有许多与请求数量相关的限制,我正在尝试对给定区域执行许多请求。

下面是一个网站,用于通过良好的 json 响应来验证 lat 和 long。

https://nationalmap.gov/epqs/

谢谢大家。

我也在这里尝试了以下示例: http://ronaldrosiernet.azurewebsites.net/Blog/2013/12/07/posting_urlencoded_key_values_with_httpclient

但是给了我以下错误。

Exception thrown: 'System.NullReferenceException'

我将代码修改为:

  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();
    }

【问题讨论】:

  • 你试过其他坐标吗?
  • 相同的结果...尽管网站 gui 用高程数据返回了良好的 json 响应。 ://
  • @MarkC. 有没有办法打印来控制我正在生成的 http 帖子。
  • 请求是不是 JSON format

标签: c# json http http-post gis


【解决方案1】:

调试一个小时后,我注意到我将单位作为参数而不是单位:( ......

    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;
        }
    }

还有baseurl如下:

private string BASEURL = "https://nationalmap.gov/epqs/pqs.php";

【讨论】:

【解决方案2】:

使用.NET HttpClient 找个例子here

【讨论】:

  • 好的,所以我使用您引用的示例将其更改为另一种方法。但是我遇到了另一个错误。
  • 空引用异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-12
  • 1970-01-01
  • 2016-06-11
相关资源
最近更新 更多