PHP で開発された REST API のサンプル コード フラグメントをいくつか書いています。「Ruby」の例を取得することはできますが、同等の適切な ASP.NET (図) の例を見つけることができませんでした。 . ASPer が、JSON 文字列をペイロードとして POST 要求を発行する次の PHP の核心的な翻訳に役立つかどうか疑問に思っていました。
注意すべき主なことは、POST には、JSON 文字列である名前付きパラメーター「data」が 1 つ必要であるということです。
$key='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // This would be your customer key
$map='USA';
$accountid='000'; // this would be your account id
// add some zips to an array
$zips[]=22201;
$zips[]=90210;
$zips[]=10001;
// We encode an array into JSON
$data = array("map" => $map, "zips"=>$zips, "accountid" => $accountid, "custkey" => $key);
$data_string = json_encode($data);
// IMPORTANT - the API takes only one POST parameter - data
$postdata="data=$data_string";
// we use curl here, but Zend has Rest interfaces as well...
$ch = curl_init('https://www.example.com//test/');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1); // make sure we submit a POST!
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$result = curl_exec($ch);
if (curl_errno($ch)) {
$result=curl_error($ch);
} else {
curl_close($ch);
}
$jsonObj=json_decode($result);
if($jsonObj->success){
$coordinates=$jsonObj->coordinates;
foreach($coordinates->coordinates as $coord){
//... application logic here ( construct XML for the map )
}
}
助けてくれてありがとう - 私はこのようなものを投稿するのは嫌いですが、将来的には他の人にも役立つかもしれません!
R
Commmentsに応えて - 私の実際の支援要求は、例をデバッグ/テストするためのASP環境の欠如から生じます。たとえば、@Chrisがリンクを投稿しましたが、額面で翻訳する際に些細なことです(以下の試み)。私の問題は、通常の投稿ファッションでデータを送信するだけではないことです。
param=val¶m2=val2
次のようにする必要があります。
data={JSONString}
ここで、JSONString は連想配列から作成されます。次に、ASPの連想配列で問題が発生します(または、明らかにその欠如ですか?- http://blog.cloudspotting.co.uk/2010/03/26/associative-arrays-in-asp-net/)一般に、次に、存在しない連想配列を JSON 文字列にエンコードする方法、または代わりに NamedValueCollection を使用しようとする場合は? また、ASP での JSON サポートにはむらがあるように見えるので、特別なインターフェイスが必要になるのではないでしょうか?
using System.Web;
Uri address = new Uri("http://www.example.com/test/");
// Create the web request
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
// Set type to POST
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
string map = "USA";
string accountid = "000";
string data ="";
NameValueCollection data = new NameValueCollection();
data.Add("custkey", key);
data.Add("map", map);
data.Add("accountid", accountid);
ASPには実際には関連配列がないため、データをJSONに変換するにはどうすればよいですか?
string jsondata="";
StringBuilder data = new StringBuilder();
UrlEncoding を使用する場合、以下の行は問題を引き起こしますか?
data.Append("data=" + HttpUtility.UrlEncode(jsondata));
// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
// Set the content length in the request headers
request.ContentLength = byteData.Length;
// Write data
using (Stream postStream = request.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
string jsonResponse =reader.ReadToEnd());
}
だから私の質問には上記のハッキングされた例を与えられるべきだと思います - 誰かがこれが正しいかどうかを教えてもらえますか、それともそうでなければ支援しますか?