2016-09-08 4 views
-2

C# 코드는 다음과 같습니다PHP SOAP POST 요청에 C# SOAP POST 요청을 번역

public static function sendSoapCurl($samlMessage, $destination, $action) { 

    $headers = array(
     "Content-type: application/soap+xml;charset=\"utf-8\"", 
     "Content-length: ".strlen($samlMessage), 
    ); 

    if (isset($action)) { 
     $headers[] = "SOAPAction: $action"; 
    } 

    // $samlMessage = utf8_encode($samlMessage); 

    // PHP cURL for https connection with auth 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
    curl_setopt($ch, CURLOPT_URL, $destination); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $samlMessage); // the SOAP request 

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

    // get soap response 
    $soapresponsexml = curl_exec($ch); 
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    curl_close($ch); 

    if ($httpCode != 200) { 
     print_r("Status code: ".$httpCode."\n"); 
     print_r($soapresponsexml);exit(); 
    } 

    if ($soapresponsexml === null || $soapresponsexml === "") { 
     throw new \Exception('Empty SOAP response, check peer certificate.'); 
    } 

    try { 
     $dom = new DOMDocument(); 
     $dom = OneLogin_Saml2_Utils::loadXML($dom, $soapresponsexml); 
    } catch (RuntimeException $e) { 
     throw new \Exception('Not a SOAP response.', 0, $e); 
    } 
    $soapfault = self::getSOAPFault($dom); 
    if (isset($soapfault)) { 
     throw new \Exception($soapfault); 
    } 
} 

: 성공 wihtout PHP에서,

private static string SendSoapRequest(string request, string destinationUrl) 
    { 
     string soapRequest = String.Format("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
              "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " + 
              "soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" + 
              "<soap:Body>{0}</soap:Body></soap:Envelope>", request); 

     HttpWebRequest req = (HttpWebRequest) WebRequest.Create(destinationUrl); 

     byte[] buffer = Encoding.UTF8.GetBytes(soapRequest); 

     req.Method = "POST"; 
     req.ContentType = "application/soap+xml"; 
     req.ContentLength = buffer.Length; 

     req.CookieContainer = new CookieContainer(); // enable cookies 
     req.Referer = "localhost"; 
     req.Credentials = CredentialCache.DefaultCredentials; 

     Stream reqst = req.GetRequestStream(); // add form data to request stream 
     reqst.Write(buffer, 0, buffer.Length); 
     reqst.Flush(); 
     reqst.Close(); 

     HttpWebResponse res = (HttpWebResponse) req.GetResponse(); 

     Stream responseStream = res.GetResponseStream(); 
     if (responseStream != null) 
     { 
      StreamReader sr = new StreamReader(responseStream); 
      string response = sr.ReadToEnd(); 
      return response; 
     } 

     return string.Empty; 
    } 

그리고 내 노력으로 지금까지, 많은 다른 변화들 C# 코드는 작동하지만 PHP에서는 작동하지 않습니다. 어떤 아이디어라도 대단히 감사하겠습니다. 감사.

+0

* * 정확하게 작동하지 않는 이유는 무엇입니까? 연구 노력을 공유하십시오. 코드에 대한 설명, 수행중인 작업 및 발생한 문제점에 대해 설명해주십시오. 수십 또는 수백 줄의 코드에서 오류를 찾기가 어렵습니다. [* 최소, 완전하고 검증 가능한 예제를 만드는 방법] (http://stackoverflow.com/help/mcve) 및 [어떻게 좋은 질문을합니까?] (http://stackoverflow.com/kr)를 참조하십시오./help/how-to-ask). –

+0

감사합니다. C#에서 PHP 로의 순수 번역에 관심이있었습니다. 내가 빠뜨린 부분은 SOAP 요청을 인코딩하는 utf8이었는데, 이는 utf8_encode ($ soapRequest) 문제를 해결했습니다. – suthers

답변

0

번역에서 utf8 인코딩 부분이 누락되었습니다. 아래 줄을 추가하면 문제가 해결되어 요청이 작동합니다.

$soapUtf8Request = utf8_encode($samlMessage); 
관련 문제