2017-02-09 5 views
1

Я пытаюсь преобразовать CURL в PHP в C#, вызов в PHP успешный, при выполнении вызова в C# он возвращает ошибку 401.CURL в php до C# .Net

C#, который производит unauthorized error (401): версия

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:

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

ответ

0

Активируйте трассировку для WebRequest добавить этот конфиг в вашем app.config И сравните это с запросом PHP

<system.diagnostics> 

    <trace autoflush="true" /> 

    <sources> 
    <source name="System.Net" maxdatasize="1024"> 
     <listeners> 
     <add name="MyTraceFile"/> 
     <add name="MyConsole"/> 
     </listeners> 
    </source> 
    </sources> 

    <sharedListeners> 
    <add name="MyTraceFile" type="System.Diagnostics.TextWriterTraceListener" initializeData="System.Net.trace.log" /> 
    <add name="MyConsole" type="System.Diagnostics.ConsoleTraceListener" /> 
    </sharedListeners> 

    <switches> 
    <add name="System.Net" value="Information" /> 
    </switches> 
</system.diagnostics> 

Дополнительная информация вы можете найти на этом blog