gpt4 book ai didi

c# - 带有 Json 响应的 Http Post C#

转载 作者:可可西里 更新时间:2023-11-01 16:36:23 24 4
gpt4 key购买 nike

我想获得一个在 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

我尝试了以下方法,但是我得到了无效的坐标,尽管它们确实存在于 map 中(尝试的示例是 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 响应。

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

最佳答案

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

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

关于c# - 带有 Json 响应的 Http Post C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43160584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com