带有 Internet 代理服务器的 Windows WCF 客户端显示错误 服务器违反了协议。部分 = ResponseStatusLine

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3839002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:20:49  来源:igfitidea点击:

Windows WCF client with internet proxy server showing error The server committed a protocol violation. Section=ResponseStatusLine

c#.netwindowswcf

提问by amexn

Our team trying to create a windows application(c#) to call a WCF service using internet proxy server

我们的团队试图创建一个 Windows 应用程序(c#)来使用 Internet 代理服务器调用 WCF 服务

Showing exception "The server committed a protocol violation. Section=ResponseStatusLine" while calling WCF service

调用 WCF 服务时显示异常“服务器违反了协议。Section=ResponseStatusLine”

Please give suggestion to solve this problem/any other alternative solution

请提出解决此问题的建议/任何其他替代解决方案

//Code for creating proxy
public static DevicesServiceClient CreateProxy()
{
  var proxy = new DevicesServiceClient("BasicHttpBinding_IDevicesService");

  BasicHttpBinding binding = new BasicHttpBinding();
  binding.Security.Mode = BasicHttpSecurityMode.None;
  binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
  binding.UseDefaultWebProxy = false;
  binding.ProxyAddress = new Uri(string.Format("http://{0}:{1}", "192.168.0.20","808"));
  proxy.Endpoint.Binding = binding;

  proxy.ClientCredentials.UserName.UserName = "Username";
  proxy.ClientCredentials.UserName.Password = "Password";
}

Server stack trace:

服务器堆栈跟踪:

at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
at ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:
at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at DevicesService.IDevicesService.CheckNetworkConnection(String ipAddress)

在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
在 ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
在 System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs,时间跨度超时)
在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System. ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage 消息)

在 [0]
处重新抛出异常:在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
at DevicesService.IDevicesService。检查网络连接(字符串 ipAddress)

My client side code in app.config alt text

我在 app.config 中的客户端代码 替代文字

My server side code in web.config alt text

我在 web.config 中的服务器端代码 替代文字

采纳答案by amexn

Code to call wcf service from windows client application using proxy server

使用代理服务器从 Windows 客户端应用程序调用 wcf 服务的代码

            {
              var proxy = new DevicesServiceClient("BasicHttpBinding_IDevicesService");
              BasicHttpBinding binding = new BasicHttpBinding("BasicHttpBinding_IDevicesService");
              var proxySettings = ApplicationDetails.CheckProxySettings();

                Uri domainAddress;
                var strtemp = new string[] { };
                //WebProxy webproxy = new WebProxy();
                var networkCredentials = new NetworkCredential();
                if (proxySettings.ProxyServerType == "http")
                {
                    domainAddress = new Uri(string.Format("http://{0}:{1}", proxySettings.ProxyServerAddress, proxySettings.ProxyServerPort));
                }
                else if (proxySettings.ProxyServerType == "https")
                {
                    domainAddress = new Uri(string.Format("https://{0}:{1}", proxySettings.ProxyServerAddress, proxySettings.ProxyServerPort));
                }
                else
                {
                    domainAddress = new Uri(string.Format("http://{0}:{1}", proxySettings.ProxyServerAddress, proxySettings.ProxyServerPort));
                }
                //
                WebProxy webproxy = new WebProxy(domainAddress.ToString(), true, strtemp);
                //

                //networkCredentials.Domain = domainAddress.ToString();
                if (proxySettings.ProxyAuthentication == "1")
                {
                    networkCredentials.UserName = proxySettings.Username;
                    networkCredentials.Password = proxySettings.Password;
                }
                webproxy.Credentials = networkCredentials;
                webproxy.BypassProxyOnLocal = false;
                WebRequest.DefaultWebProxy = webproxy;
                binding.UseDefaultWebProxy = true;
                proxy.Endpoint.Binding = binding;

            }

回答by Ladislav Mrnka

I haven't seen this exception before but I already had big problems to make proxy work this way. I don't know why but setting proxy address and use default proxy to false in BasicHttpBinding never worked for me. I always had to use default web proxy and set the URL there or create whole new custom binding and set proxy URL in http transport binding element.

我以前从未见过这个异常,但我已经遇到了让代理以这种方式工作的大问题。我不知道为什么,但是在 BasicHttpBinding 中设置代理地址并使用默认代理为 false 从来没有对我来说有效过。我总是不得不使用默认的 Web 代理并在那里设置 URL 或创建全新的自定义绑定并在 http 传输绑定元素中设置代理 URL。

回答by ivymike

Try checking the headers returned by your proxy server. It looks like malformed/non standard headers cause this exception.

尝试检查代理服务器返回的标头。看起来格式错误/非标准标头会导致此异常。

http://www.velocityreviews.com/forums/t302174-why-do-i-get-the-server-committed-a-protocol-violation.html

http://www.velocityreviews.com/forums/t302174-why-do-i-get-the-server-committed-a-protocol-violation.html

回答by Jacob Proffitt

It looks like this is something of a catch-all exception and has limited utility as a result. Malformed headers, hitting the wrong port (that returns a different formatted response), and content-length in the response can all be causes for the exception. If it's a header issue, you can tell the client to ignore unsafe headers (if it's a trusted source).

看起来这是一个包罗万象的异常,因此实用性有限。格式错误的标头、访问错误的端口(返回不同格式的响应)和响应中的内容长度都可能是导致异常的原因。如果是标头问题,您可以告诉客户端忽略不安全的标头(如果它是可信来源)。

<configuration>
    <system.net>
        <settings>
            <httpWebRequest useUnsafeHeaderParsing="true" />
        </settings>
    </system.net>
</configuration>

回答by softveda

Try to diagnose this one step at a time.

尝试一次诊断这一步。

  1. Change the servicebehavior to add <serviceMetadata httpGetEnabled = "true"/>Then change the proxy setting in IE and browse to the service url and see if you can get the WSDL page.
  2. Do the above with SoapUIand see if there are any errors. If WSDL is available generate proxy and call the operation to check for errors.
  3. Enable WCF tracing as described here. The tracelogs are very detailed and I have diagnosed many WCF issues using this technique.
  4. Finally try network tracing using WireShark.
  1. 更改servicebehavior添加<serviceMetadata httpGetEnabled = "true"/>然后在IE中更改代理设置,浏览到服务url,看看是否可以获取WSDL页面。
  2. 使用SoapUI执行上述操作,看看是否有任何错误。如果 WSDL 可用,则生成代理并调用操作以检查错误。
  3. 此处所述启用 WCF 跟踪。跟踪日志非常详细,我使用这种技术诊断了许多 WCF 问题。
  4. 最后尝试使用WireShark 进行网络跟踪。

回答by Eilistraee

Have you tried to use a network trafic analyser to get more informations about what is sent and received by your client and server? This could help you solving the issue, or perhaps at least identifying the root cause.

您是否尝试过使用网络流量分析器来获取有关客户端和服务器发送和接收的内容的更多信息?这可以帮助您解决问题,或者至少可以确定根本原因。