6
votes

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

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

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)

My client side code in app.config alt text

My server side code in web.config alt text

6

6 Answers

2
votes

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.

2
votes

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

2
votes

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>
2
votes

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 SoapUI and 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.
2
votes

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

            {
              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;

            }
1
votes

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.