1
PHPでCURLをC#に変換しようとしていますが、PHPでの呼び出しは成功しています.C#で呼び出しを行うと、401エラーが返されます。CURL in PHP to C#.Net
var data = new JavaScriptSerializer().Serialize(new { amount = "29.07" });
string url = "https://devapice.vnforapps.com/api.ecommerce/api/v1/ecommerce/token/101340XXX";
HttpWebRequest q = (HttpWebRequest)WebRequest.Create(url);
q.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
q.PreAuthenticate = true;
q.Method = "POST";
q.ContentType = "application/json";
q.Headers.Add("VisaNet-Session-Key", new Guid().ToString());
q.Credentials = new NetworkCredential("XXXAILCA2WW4PT2QHXXX", "XXXi0ZVrIcmtp8jVhCYSn0H9KcprJHVOTBKvKXXX");
byte[] buffer = Encoding.UTF8.GetBytes(data);
q.ContentLength = buffer.Length;
using (Stream stream = q.GetRequestStream())
{
stream.Write(buffer, 0, buffer.Length);
}
HttpWebResponse reps = q.GetResponse() as HttpWebResponse;
Console.Write(reps.ToString());
ワーキングPHPのバージョン:unauthorized error (401)
生成
C#の
$sessionToken = getGUID();
$url = "https://devapice.vnforapps.com/api.ecommerce/api/v1/ecommerce/token/1480XXXXX";
$header = array("Content-Type: application/json","VisaNet-Session-Key: $sessionToken");
$request_body="{
\"amount\":1.00
}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "XXXAILCA2WW4PT2QHXXX:XXXa4Zg3ltemrcNG7pLergqwwGlYJH+rahejFXXX");
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $request_body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($ch);
$json = json_decode($response);
$dato = $json->sessionKey;
function getGUID(){
if (function_exists('com_create_guid')){
return com_create_guid();
}else{
mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
$charid = strtoupper(md5(uniqid(rand(), true)));
$hyphen = chr(45);// "-"
$uuid = chr(123)// "{"
.substr($charid, 0, 8).$hyphen
.substr($charid, 8, 4).$hyphen
.substr($charid,12, 4).$hyphen
.substr($charid,16, 4).$hyphen
.substr($charid,20,12).$hyphen
.chr(125);// "}"
$uuid = substr($uuid, 1, 36);
return $uuid;
}
}