0
我正在從一個工作的C#類到PHP類的功能端口 我tryinhg做一個非常簡單的任務,但性能是不同的,我找不到問題,也許你們可以幫忙。PHP cURL問題...錯誤401
這是C#代碼(不是我的工作原樣,敏感數據被隱藏):
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代碼:
$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#代碼給狀態正常和有效認證令牌。 PHP代碼給狀態401和錯誤:用戶不存在。
我不能說爲什麼不同。 我比較了兩者上的請求和響應頭,並且所有內容看起來都一樣。
請幫忙。