2017-04-01 14 views
2

私は、C#で次の設定でhttp投稿を生成するメソッドを取得したいと思います。Http投稿C#with 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と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; 
     } 
    } 
} 

私は取得していますエラーは以下の通りです:

​​

はまたいずれかを持っています1人は.govウェブサイトで標高データを取得しようとしました。私はGoogleのサーバーを使用していますが、リクエストの数に関連する多くの制限があり、特定のエリアに対して多くのリクエストを実行しようとしています。

以下は、良い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 lso baseurlは次のとおりです。

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

*** HttpClient ***と*** WebClient? – Kiquenet

0

使用.NET HttpClient を例にhere

+0

だから、参照した例を使って別のメソッドに変更しました。しかし、別のエラーが発生しています。 – nader

+0

どのエラーが表示されますか? – McKabue

+0

Nullreferenceexception – nader

関連する問題