Windows WCF客户端使用互联网代理服务器时显示错误:服务器违反了协议。部分=ResponseStatusLine。

6
我们的团队正在尝试创建一个使用Internet代理服务器调用WCF服务的Windows应用程序(C#)。
在调用WCF服务时,出现了异常“服务器违反了协议。Section=ResponseStatusLine”。
请提供解决此问题/任何其他替代方案的建议。
//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";
}

服务器堆栈跟踪:

在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)中
在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message,TimeSpan timeout)中
在System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway, ProxyOperationRuntime operation,Object [] ins,Object [] outs,TimeSpan timeout)中
在System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway, ProxyOperationRuntime operation,Object [] ins,Object [] outs)中
在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime operation)中
在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)中

异常在[0]处重新抛出:
在System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)中
在System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData,Int32 type)中
在DevicesService.IDevicesService.CheckNetworkConnection(String ipAddress)中

我的客户端代码在app.config中 alt text

我的服务器端代码在web.config中 alt text

6个回答

2

使用代理服务器从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;

            }

2

看起来这似乎是一个全能异常,因此其效用有限。格式错误的标头、命中不正确的端口(返回不同格式的响应)以及响应中的内容长度都可能是异常的原因。如果是标头问题,您可以告诉客户端忽略不安全的标头(如果它是可信源)。

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

不是头部的问题。我的下一个怀疑对象将是内容长度。检查服务器和客户端上的绑定配置,以获取MaxXXXMessageSize(XXX = Received或Sent),并将它们提高。 - Jacob Proffitt
现在客户端的 app.config 和服务器端的 web.config 代码显示在问题中。 - amexn
现在我在客户端增加了maxReceivedMessageSize和maxBufferSize属性的值,但仍然显示相同的错误。 - amexn
你能在服务器端做同样的操作吗?当这个问题困扰我时,我恍然大悟了... - Jacob Proffitt
<bindings> <basicHttpBinding> <binding name="basicHttpBinding" maxReceivedMessageSize="2147483647"> <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/> - Jacob Proffitt

2

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


请提供解决这个问题的代码/参考,您在回答中已经提到了“使用默认Web代理并在那里设置URL或创建全新的自定义绑定并在http传输绑定元素中设置代理URL”。 - amexn

2

2

尝试逐步诊断此问题。

  1. 更改servicebehavior以添加 <serviceMetadata httpGetEnabled = "true"/>,然后更改IE中的代理设置并浏览到服务url,查看是否可以获取WSDL页面。
  2. 使用SoapUI执行上述操作,并检查是否有任何错误。如果WSDL可用,请生成代理并调用操作以检查错误。
  3. 按照此处描述启用WCF跟踪。跟踪日志非常详细,我已经使用这种技术诊断了许多WCF问题。
  4. 最后,尝试使用WireShark进行网络跟踪。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接