我有一个我试图在服务器上运行的 Windows 服务。它只是将数据发布到 php 页面,并根据响应返回进一步的逻辑。当为网站(IIS)打开匿名身份验证时,这在我的机器上 100% 有效,在服务器上 100% 有效。
我想要完成的是在打开 Windows 身份验证的情况下运行该站点并让我的服务仍然工作......除非我尝试这样做,否则我会收到一个不错的 EventLog 条目。
“System.IO.IOException:无法从传输连接读取数据:现有连接被远程主机强行关闭”
我在具有所有必需权限的帐户下运行该服务。所以我知道“CredentialCache.DefaultCredentials”应该使用这个帐户进行身份验证(我没有收到 401 错误,但不排除它)
下面是我的 HttpPost 代码,我从其他帖子中设置了很多选项,试图找到解决方案。
string postData = xml;
//--------------
HttpRequestCachePolicy policy = new HttpRequestCachePolicy MontgP02(HttpRequestCacheLevel.NoCacheNoStore);
HttpWebRequest.DefaultCachePolicy = policy;
byte[] buffer = Encoding.UTF8.GetBytes(postData);
//Initialisation
System.Net.ServicePointManager.Expect100Continue = false;
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
//Credentials
WebReq.UseDefaultCredentials = true;
WebReq.PreAuthenticate = true;
WebReq.Credentials = CredentialCache.DefaultCredentials;
//Request Settings
WebReq.ProtocolVersion = HttpVersion.Version11;
//WebReq.ServicePoint.ConnectionLimit = 1;
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
//The length of the buffer
WebReq.ContentLength = buffer.Length;
//Set timeout
WebReq.Timeout = 25000;
//Use Proxy or Not
if (useproxy == "ON")
{
WebProxy myProxy = new WebProxy();
// Create a new Uri object.
Uri newUri = new Uri(proxy);
// Associate the new Uri object to the myProxy object.
myProxy.Address = newUri;
WebReq.Proxy = myProxy;
}
try
{
//Send Data
using (Stream requestStream = WebReq.GetRequestStream())
{
// write to stream
//write, and close.
requestStream.Write(buffer, 0, buffer.Length);
}
}
catch (WebException ex) {
MessageLog("ERROR: Sending Data : " + ex.ToString() + ":", "ERROR");
}
//Get Response
try
{
using (System.Net.WebResponse response = WebReq.GetResponse())
{
Stream Answer = response.GetResponseStream();
// read from stream
StreamReader _Answer = new StreamReader(Answer);
PostResponse = _Answer.ReadToEnd();
}
}
catch (WebException ex)
{
MessageLog("ERROR: Receiving Response : " + ex.ToString() + ":" , "ERROR");
}
}
catch (WebException ex)
{
MessageLog("ERROR Posting " + ex.ToString() + ":" + PostResponse, "ERROR");
}
}
我从错误中了解到,在我完成请求之前服务器正在切断连接。每次在服务器上都会发生这种情况,这否定了我认为的超时想法。环顾其他四个方面,我不知所措,尝试了其他变体等,事实上它在关闭身份验证的情况下确实可以正常工作,这让我认为它可能是 IIS 中的设置或身份验证路径中的其他东西。
欢迎任何见解