[英]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.