[英]NTLM request in C# and php
我在C#中有一個ntlm請求可以工作,而在php中有一個相同的請求可以連接,但是在響應中返回錯誤。
這是c#代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://example.com/Query/ExecuteReportView");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
httpWebRequest.UseDefaultCredentials = false;
httpWebRequest.PreAuthenticate = true;
var c = new NetworkCredential("username", "password", "domain");
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri("http://example.com"), "NTLM", c);
httpWebRequest.Credentials = credentialCache;
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"SysName\":\"Callbacks\"," +
"\"SysCategory\":\"Eai.CCM.Model.FolderView\"," +
"\"StartIndex\":0," +
"\"PageLength\":10}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
Console.Out.WriteLine(result);
}
Console.ReadLine();
}
}
}
可以正常工作並返回預期的json字符串。
這是我在php中做同樣的嘗試:
<?php
$data = array
(
"SysName" => "Callbacks",
"SysCategory" => "Eai.CCM.Model.FolderView",
"StartIndex" => 0,
"PageLength" => 10
);
$body = json_encode($data);
$headers = array(
"Transfer-Encoding: chunked",
"Content-Type: application/json"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, 'example.com/Query/ExecuteReportView');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_FAILONERROR, 0);
curl_setopt($ch, CURLOPT_MAXREDIRS, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, dechex(strlen($body)) . "\r\n" . $body . "\r\n");
curl_exec($ch);
curl_close($ch);
?>
一個重大區別是,php代碼使用分塊編碼,因為內容長度方法不斷給我一個錯誤,告訴我我需要指定內容長度或分塊發送數據。
有什么明顯的優勢讓我在php腳本中錯過的人脫穎而出嗎?
php腳本連接並返回響應,因此我顯然沒有提供正確的信息(就像我在C#版本中一樣)。
這是php版本返回的內容:
{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"Object reference not set to an instance of an object.","ExceptionType":"System.Exception","StackTrace":null}
我正在使用PHP 5.6.6。
編輯:
我的php腳本使用內容長度而不是分塊編碼數據在php 5.2.17中工作
PHP 5.5.21中的相同問題
phpinfo為5.2.17提供了以下內容:
cURL support enabled
cURL Information libcurl/7.21.0 OpenSSL/0.9.8q zlib/1.2.3
和以下5.6.6
cURL support enabled
cURL Information 7.40.0
Age 3
Features
AsynchDNS Yes
CharConv No
Debug No
GSS-Negotiate No
IDN Yes
IPv6 Yes
krb4 No
Largefile Yes
libz Yes
NTLM Yes
NTLMWB No
SPNEGO Yes
SSL Yes
SSPI Yes
TLS-SRP No
Protocols dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp
Host i386-pc-win32
SSL Version OpenSSL/1.0.1k
ZLib Version 1.2.7.3
libSSH Version libssh2/1.4.3
終於得到了答案:
似乎IIS正在為我做事。 似乎發送標頭“ Transfer-Encoding:chunked”告訴IIS為我做分塊。 因此,我不需要自己對數據進行分塊。
<?php
$data = array
(
"SysName" => "Callbacks",
"SysCategory" => "Eai.CCM.Model.FolderView",
"StartIndex" => 0,
"PageLength" => 10
);
$body = json_encode($data);
$headers = array(
"Transfer-Encoding: chunked",
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERPWD, 'username:password');
curl_setopt($ch, CURLOPT_URL, 'example.com/Query/ExecuteReportView');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_exec($ch);
curl_close($ch);
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.