title: WCF The Remote Server Returned an Unexpected Response: (400) Bad Request
body: <p>I use the <code>basicHttpBinding</code> protocol for WCF.</p>
  
  <p>I receive large <code>Class&lt;T&gt;</code> (not problem).</p>
  
  <p>But I send large <code>Class&lt;T&gt;</code> I get this error</p>
  
  <blockquote>
    <p>Exception thrown: 'System.ServiceModel.ProtocolException' in
    System.ServiceModel.Internals.dll</p>
    
    <p>Additional information: The remote server returned an unexpected
    response: (400) Bad Request.</p>
  </blockquote>
  
  <p>My server config is</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
    &lt;system.diagnostics&gt;
      &lt;sources&gt;
        &lt;source propagateActivity="true" 
                name="System.ServiceModel"
                switchValue="Warning,ActivityTracing"&gt;
          &lt;listeners&gt;
            &lt;add type="System.Diagnostics.DefaultTraceListener"
                 name="Default"&gt;
              &lt;filter type="" /&gt;
            &lt;/add&gt;
            &lt;add name="ServiceModelTraceListener"&gt;
              &lt;filter type="" /&gt;
            &lt;/add&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
        &lt;source name="System.ServiceModel.MessageLogging"
                switchValue="Information, ActivityTracing"&gt;
          &lt;listeners&gt;
            &lt;add type="System.Diagnostics.DefaultTraceListener"
                 name="Default"&gt;
              &lt;filter type="" /&gt;
            &lt;/add&gt;
            &lt;add name="ServiceModelMessageLoggingListener"&gt;
              &lt;filter type="" /&gt;
            &lt;/add&gt;
          &lt;/listeners&gt;
        &lt;/source&gt;
      &lt;/sources&gt;
      &lt;sharedListeners&gt;
        &lt;add initializeData="d:\tourism\tourism\server\service\deltaservicewcf\web_messages.svclog"
          type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
          name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp"&gt;
          &lt;filter type="" /&gt;
        &lt;/add&gt;
        &lt;add initializeData="d:\tourism\tourism\server\service\deltaservicewcf\web_tracelog.svclog"
          type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
          name="ServiceModelTraceListener" traceOutputOptions="Timestamp"&gt;
          &lt;filter type="" /&gt;
        &lt;/add&gt;
      &lt;/sharedListeners&gt;
      &lt;trace autoflush="true" /&gt;
    &lt;/system.diagnostics&gt;
    &lt;system.serviceModel&gt;
      &lt;diagnostics wmiProviderEnabled="false"&gt;
        &lt;messageLogging logEntireMessage="true" logMalformedMessages="true"
          logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true" /&gt;
      &lt;/diagnostics&gt;
      &lt;bindings&gt;
        &lt;basicHttpBinding&gt;
          &lt;binding name="NewBinding0" closeTimeout="01:01:00" openTimeout="01:01:00"
            receiveTimeout="01:10:00" sendTimeout="01:01:00" maxBufferPoolSize="2147483647"
            maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
            transferMode="Streamed"&gt;
            &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
              maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
          &lt;/binding&gt;
        &lt;/basicHttpBinding&gt;
      &lt;/bindings&gt;
      &lt;services&gt;
        &lt;service behaviorConfiguration="myHttpBindingBihaviour" name="DeltaServiceWCF.DeltaService1"&gt;
          &lt;endpoint behaviorConfiguration="myHttpBindingEndPointBihaviour"
            binding="basicHttpBinding" bindingConfiguration="" name="" contract="DeltaServiceWCF.IDeltaService1"
            isSystemEndpoint="false" /&gt;
          &lt;host&gt;
            &lt;timeouts closeTimeout="01:00:10" openTimeout="01:01:00" /&gt;
          &lt;/host&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior name="myHttpBindingBihaviour"&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
            &lt;serviceTimeouts transactionTimeout="01:00:01" /&gt;
            &lt;dataContractSerializer ignoreExtensionDataObject="true"  maxItemsInObjectGraph="2147483647"/&gt;
            &lt;serviceThrottling maxConcurrentCalls="2000" maxConcurrentSessions="1000"  maxConcurrentInstances="1000" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
        &lt;endpointBehaviors&gt;
          &lt;behavior name="myHttpBindingEndPointBihaviour" &gt;
            &lt;dataContractSerializer ignoreExtensionDataObject="true" maxItemsInObjectGraph="2147483647"/&gt;
          &lt;/behavior&gt;
        &lt;/endpointBehaviors&gt;
      &lt;/behaviors&gt;
      &lt;!--&lt;protocolMapping&gt;
        &lt;add binding="basicHttpsBinding" scheme="https" /&gt;
      &lt;/protocolMapping&gt;--&gt;
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true"
        multipleSiteBindingsEnabled="true" /&gt;
      &lt;standardEndpoints&gt;
        &lt;webHttpEndpoint&gt;
          &lt;standardEndpoint name="" maxBufferSize="2147483647" maxBufferPoolSize="4194304"
            maxReceivedMessageSize="2147483647" helpEnabled="true" automaticFormatSelectionEnabled="true"
            crossDomainScriptAccessEnabled="true"&gt;
            &lt;readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
              maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /&gt;
          &lt;/standardEndpoint&gt;
        &lt;/webHttpEndpoint&gt;
      &lt;/standardEndpoints&gt;
  
    &lt;/system.serviceModel&gt;
    &lt;system.web&gt;
      &lt;httpRuntime maxRequestLength="2147483647" maxQueryStringLength="2097151" maxUrlLength="2097151" maxWaitChangeNotification="2147483647" requestValidationMode="4.0" executionTimeout="110000" targetFramework="4.5"/&gt;
      &lt;compilation debug="true"/&gt;
      &lt;customErrors mode="Off"/&gt;
    &lt;/system.web&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
      &lt;!--
          To browse web app root directory during debugging, set the value below to true.
          Set to false before deployment to avoid disclosing web app folder information.
        --&gt;
      &lt;directoryBrowse enabled="true"/&gt;
  
    &lt;/system.webServer&gt;
  
    &lt;!--&lt;system.web.extensions&gt;
      &lt;scripting&gt;
        &lt;webServices&gt;
          &lt;authenticationService enabled="true"
              requireSSL="false"  /&gt;
        &lt;/webServices&gt;
      &lt;/scripting&gt;
    &lt;/system.web.extensions&gt;--&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>My client code is</p>
  
  <pre><code>public static EndpointAddress EndPoint = new EndpointAddress("http://192.168.1.102:9159/DeltaService1.svc");
  
  public static BasicHttpBinding CreateBasicHttp()
  {
  
      var binding = new BasicHttpBinding();
      binding.MaxReceivedMessageSize = 2147483647;
      binding.MessageEncoding = WSMessageEncoding.Text;
      binding.ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
      {
          MaxArrayLength = 2147483647,
          MaxStringContentLength = 2147483647,
          MaxBytesPerRead = 2147483647,
          MaxDepth = 2147483647,
          MaxNameTableCharCount = 2147483647,
      };
      binding.MaxBufferPoolSize = 2147483647;
      binding.MaxBufferSize = 2147483647;
      binding.TransferMode = TransferMode.Streamed;
      binding.Security = new BasicHttpSecurity();
  
      binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
  
      binding.Security.Mode = BasicHttpSecurityMode.None;
      binding.Security.Transport = new HttpTransportSecurity();
      binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
      binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
      binding.Security.Transport.Realm = "";
  
      var timeout = new TimeSpan(1, 1, 0);
      binding.SendTimeout = timeout;
  
      binding.CloseTimeout = timeout;
      binding.OpenTimeout = timeout;
      binding.ReceiveTimeout = timeout;
      binding.SendTimeout = timeout;
      binding.TextEncoding = Encoding.UTF8;
  
      ChannelFactory&lt;IDeltaService1&gt; factory = new ChannelFactory&lt;IDeltaService1&gt;(binding, EndPoint);
  
      foreach (OperationDescription op in factory.Endpoint.Contract.Operations)
      {
          var dataContractBehavior = op.Behaviors.Find&lt;DataContractSerializerOperationBehavior&gt;();
          if (dataContractBehavior != null)
          {
              dataContractBehavior.IgnoreExtensionDataObject = true;
              dataContractBehavior.MaxItemsInObjectGraph = int.MaxValue;
          }
      }
  
      IDeltaService1 channel = factory.CreateChannel();
  
      return binding;
  }
  </code></pre>