2016-07-17 3 views
0

작동중인 C# 클래스의 기능 포트를 PHP 클래스로 만듭니다. 매우 간단한 작업을 수행하려고 tryinhg하지만 성능이 다르며 찾을 수 없습니다. 문제는, 어쩌면 너희들도 도울 수있어.PHP cURL 문제 ... 오류 401

$ch = curl_init(); 
    $url = "https://some_server/Login"; 
    $host = parse_url($url, PHP_URL_HOST); 
    //$path = parse_url($url, PHP_URL_PATH); 

    $xml_data = "<?xml version='1.0' ?>". 
       "<authorization>". 
       "<username><![CDATA[some_name]]></username>". 
       "<password><![CDATA[some_password]]></password>". 
       "<domain><![CDATA[some_domain]]></domain>". 
       "</authorization>"; 

    $headers = array(
      "Content-type: text/xml", 
      "Accept: */*,text/xml", 
      "User-Agent: dsaxess/special", 
      "Host: ".$host, 
      "Content-length: ".strlen($xml_data), 
      "Expect: 100-continue", 
      "Connection: Keep-Alive" 
    ); 

    //var_dump($headers); 
    //var_dump($xml_data); 

    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch,CURLOPT_BINARYTRANSFER,true); 

    curl_setopt($ch, CURLOPT_HEADER, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); 

    $data = curl_exec($ch); 

    if (curl_errno($ch)) { 
     print "Error: " . curl_error($ch); 
    } else { 
     var_dump(curl_getinfo($ch,CURLINFO_HTTP_CODE)); 
     //var_dump($data); 
     curl_close($ch); 
    } 

C# 코드 OK 상태를 제공하고 유효한 :

 Stream requestStream = null; 
     HttpWebRequest webRequest = null; 
     StringBuilder postBuilder = null; 
     HttpWebResponse webResponse = null; 
     string requestUrl = null; 
     string cookieString = null; 
     byte[] rawPostData = null; 

     requestUrl = "https://some_server/Login"; 
     webRequest = (HttpWebRequest)HttpWebRequest.Create(requestUrl); 
     postBuilder = new StringBuilder(); 

     postBuilder.Append("<?xml version=\"1.0\" ?>"); 
     postBuilder.Append("<authorization>"); 
     postBuilder.Append("<username><![CDATA[some_name]]></username>"); 
     postBuilder.Append("<password><![CDATA[some_password]]></password>"); 
     postBuilder.Append("<domain><![CDATA[some_domain]]></domain>"); 
     postBuilder.Append("</authorization>"); 

     rawPostData = Encoding.Default.GetBytes(postBuilder.ToString()); 

     webRequest.ContentType = "text/xml"; 
     webRequest.Accept = "*/*,text/xml"; 
     webRequest.ContentLength = rawPostData.Length; 
     webRequest.Method = "POST"; 
     webRequest.UserAgent = "dsaxess/special"; 

     requestStream = webRequest.GetRequestStream(); 
     requestStream.Write(rawPostData, 0, rawPostData.Length); 
     requestStream.Flush(); 


     webResponse = (HttpWebResponse)webRequest.GetResponse(); 
     Debug.WriteLine("Status: " + webResponse.StatusCode); 
     //Debug.Write(webRequest.Headers); 
     //Debug.Write(webResponse.Headers); 
     requestStream.Close(); 
     requestStream = null; 
     postBuilder = null; 

     Debug.WriteLine("Done!"); 

이 내 PHP 코드입니다 :

는 (그대로, 중요한 데이터는 숨겨 작동하지 광산) C# 코드입니다 인증 토큰. PHP 코드 상태 401 및 오류 : 사용자가 존재하지 않습니다.

왜 다른지 알 수 없습니다. 둘 모두의 요청 및 응답 헤더를 비교하고 모든 것이 동일하게 보입니다.

도와주세요.

답변

2

직접 해결. 헤더 구문이 잘못되었습니다. 올바른 구문은 다음과 같습니다.

$headers = array(
      "Content-type" => "text/xml", 
      "Accept" => "*/*,text/xml", 
      "User-Agent" => "dsaxess/special", 
      "Host" => $host, 
      "Content-length" => strlen($xml_data), 
      "Expect" => "100-continue", 
      "Connection" => "Keep-Alive" 
    );