Windows 服务上的 C# HttpWebRequest:更改请求中的任何 属性 不会产生效果
C# HttpWebRequest on Windows Service: Changing any Property in Request does't give a Effect
我写了一个 Windows WCF 服务(运行 作为本地系统)从网络服务器获取请求,其中一个数据验证步骤是,将它们再次发送到网络服务器 (https ),它正在做这项工作。
现在我正在尝试使用 "HttpWebRequest" class 发送请求。
以下执行我的请求:(参数:字符串 a_Body 包含字符串化 Json )
// Accept all certificates
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
{
return true;
};
// Encode given Json coded Data to Bytearray
byte[] formData = Encoding.UTF8.GetBytes( a_Body );
HttpWebRequest req = WebRequest.Create(new Uri( ApiVerifyUrl )) as HttpWebRequest;
req.KeepAlive = false;
req.Method = "POST";
req.Accept = "application/json";
req.ContentType = "application/json";
// Test Header, to see, if it would be set!
req.Headers.Add("X-Test", "Test123");
req.ContentLength = formData.Length;
// fiddler connection
req.Proxy = new WebProxy("[ip]", [port]);
using (Stream post = req.GetRequestStream()) // <- throws error ( see log below )
{
post.Write( formData, 0, formData.Length );
post.Close();
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
我也捕获了一个异常,whis 被抛出:
using (Stream post = req.GetRequestStream())
异常详情:
[21.11.2018 08:36:21] Emergency:
An exception "WebException" occurred:
- Message:
Die zugrunde liegende Verbindung wurde geschlossen: Unerwarteter Fehler beim Senden..
(= The Connection was closed: unexpected error)
- Source:
System
- Target:
System.IO.Stream GetRequestStream(System.Net.TransportContext ByRef)
- Stack Trace:
bei System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
bei System.Net.HttpWebRequest.GetRequestStream()
bei Intrasearch.Service.HttpServer.ValidateRequestByApi(String a_Body)
- InnerException: "IOException":
- Message:
Von der Übertragungsverbindung können keine Daten gelesen werden: Eine vorhandene Verbindung wurde vom Remotehost geschlossen.
(= Data cant be read from the Connection: Remotehost closed the connection)
- Source:
System
- Target:
Int32 Read(Byte[], Int32, Int32)
- Stack Trace:
bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
bei System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
bei System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
bei System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
bei System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
bei System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
bei System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
bei System.Net.ConnectStream.WriteHeaders(Boolean async)
- InnerException: "SocketException":
- Message:
Eine vorhandene Verbindung wurde vom Remotehost geschlossen
(= An Connection was closed by the remotehost)
- Source:
System
- Target:
Int32 Read(Byte[], Int32, Int32)
- Stack Trace:
bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
现在我要问:
为什么我的Attributes没有设置好,导致请求无法正常建立连接?
相同的代码在具有相同 Web 服务器的控制台应用程序中运行。
我现在正在搜索几天。
我使用了不支持 Tls 1.2 的旧版本 .Net Framework (4.0)。
我们的服务器限制了任何没有 Tls 1.2 的连接,所以发生了这个错误。
我只是将 .Net 版本更改为 4.5 并添加了:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
现在可以使用了!
我写了一个 Windows WCF 服务(运行 作为本地系统)从网络服务器获取请求,其中一个数据验证步骤是,将它们再次发送到网络服务器 (https ),它正在做这项工作。
现在我正在尝试使用 "HttpWebRequest" class 发送请求。
以下执行我的请求:(参数:字符串 a_Body 包含字符串化 Json )
// Accept all certificates
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
{
return true;
};
// Encode given Json coded Data to Bytearray
byte[] formData = Encoding.UTF8.GetBytes( a_Body );
HttpWebRequest req = WebRequest.Create(new Uri( ApiVerifyUrl )) as HttpWebRequest;
req.KeepAlive = false;
req.Method = "POST";
req.Accept = "application/json";
req.ContentType = "application/json";
// Test Header, to see, if it would be set!
req.Headers.Add("X-Test", "Test123");
req.ContentLength = formData.Length;
// fiddler connection
req.Proxy = new WebProxy("[ip]", [port]);
using (Stream post = req.GetRequestStream()) // <- throws error ( see log below )
{
post.Write( formData, 0, formData.Length );
post.Close();
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
我也捕获了一个异常,whis 被抛出:
using (Stream post = req.GetRequestStream())
异常详情:
[21.11.2018 08:36:21] Emergency:
An exception "WebException" occurred:
- Message:
Die zugrunde liegende Verbindung wurde geschlossen: Unerwarteter Fehler beim Senden..
(= The Connection was closed: unexpected error)
- Source:
System
- Target:
System.IO.Stream GetRequestStream(System.Net.TransportContext ByRef)
- Stack Trace:
bei System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
bei System.Net.HttpWebRequest.GetRequestStream()
bei Intrasearch.Service.HttpServer.ValidateRequestByApi(String a_Body)
- InnerException: "IOException":
- Message:
Von der Übertragungsverbindung können keine Daten gelesen werden: Eine vorhandene Verbindung wurde vom Remotehost geschlossen.
(= Data cant be read from the Connection: Remotehost closed the connection)
- Source:
System
- Target:
Int32 Read(Byte[], Int32, Int32)
- Stack Trace:
bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
bei System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
bei System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
bei System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
bei System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
bei System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
bei System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
bei System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
bei System.Net.ConnectStream.WriteHeaders(Boolean async)
- InnerException: "SocketException":
- Message:
Eine vorhandene Verbindung wurde vom Remotehost geschlossen
(= An Connection was closed by the remotehost)
- Source:
System
- Target:
Int32 Read(Byte[], Int32, Int32)
- Stack Trace:
bei System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
现在我要问: 为什么我的Attributes没有设置好,导致请求无法正常建立连接? 相同的代码在具有相同 Web 服务器的控制台应用程序中运行。 我现在正在搜索几天。
我使用了不支持 Tls 1.2 的旧版本 .Net Framework (4.0)。 我们的服务器限制了任何没有 Tls 1.2 的连接,所以发生了这个错误。
我只是将 .Net 版本更改为 4.5 并添加了:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
现在可以使用了!