C#に変換しようとしている次のCURLがあります。私はCURLの経験がまったくありません。
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => 'https://api.lanoba.com/authenticate',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => array(
'token' => $_POST['token'],
'api_secret' => 'YOUR-API-SECRET'
)
));
これまでのところ、私はこれを思いついた:
//Object to create a JSON object
public class LanobaJSONObject
{
public string token { get; set; }
public string api_secret { get; set; }
}
public void DoAuthenticationCheck()
{
var token = Request["token"].ToString();
var jsonObject = new LanobaJSONObject()
{
token = token,
api_secret = "YOUR-API-SECRET"
};
var jsonVal = Json(jsonObject, JsonRequestBehavior.AllowGet);
Uri address = new Uri("https://api.lanoba.com/authenticate");
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);
ServicePointManager.ServerCertificateValidationCallback = delegate
{
return
true; //always trust the presented cerificate
};
request.Method = "post";
request.ContentType = "text/json";
string response = null;
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(jsonVal);
}
using (HttpWebResponse resp = request.GetResponse() as HttpWebResponse)
{
var reader = new StreamReader(resp.GetResponseStream());
response = reader.ReadToEnd();
}
//I keep getting an error code back from the provider with no real error description
//so right now I am assuming that I am doing something wrong on my end
}
どんな助けでもいただければ幸いです。
編集:最終回答:
Onkelborgの助けを借りて(ありがとう!)、実際の例を次に示します。
var wc = new WebClient();
var wcResponse = wc.UploadValues("https://api.lanoba.com/authenticate", new System.Collections.Specialized.NameValueCollection() { { "token", Request["token"].ToString()}, { "api_secret", "Your-Secret-Api--" } });
var decodedResponse = wc.Encoding.GetString(wcResponse);
もう一度、ありがとうございます。