2017-04-01 114 views
2

我想獲得一個方法,在c#中使用以下配置生成http文章。Http發佈C#與Json響應

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和經度= -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; 
     } 
    } 
} 

我得到的錯誤是如下:

​​

而且具有任何一個人嘗試使用.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(); 
    } 
+0

您是否試過其他座標? –

+0

同樣的結果...雖然網站gui給出了一個很好的json響應和高程數據。 :/ – nader

+0

@ MarkC.is有一種打印方式來控制我生成的http文章。 – nader

回答

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

A如下baseurl如下:

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

*** HttpClient *** vs *** WebClient? – Kiquenet

0

使用.NET HttpClient 找到一個例子here

+0

好吧所以我使用您引用的示例將其更改爲另一種方法。但是我收到另一個錯誤。 – nader

+0

你收到哪個錯誤? – McKabue

+0

Nullreferenceexception – nader