'HOW TO FIX: The remote server returned an error: (400) Bad Request

I'm trying to fix this error, I know that there are another similar questions with the same error, but the line: request.ContentLength = 0; it doesn't fix my exception.

This is my image code:

public ActionResult Index()
{
    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-US");
    Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
    string serviceUrl = string.Format(@"http://localhost:61940/Service1.svc/GetPlayer/");

    HttpWebRequest request = (HttpWebRequest) WebRequest.Create(serviceUrl);
    request.Method = "POST";
    request.ContentType = @"application/json; charset=utf-8";
    request.ContentLength = 0;

    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    Stream stream = request.GetRequestStream();
    StreamWriter reader = new StreamWriter(stream);
}

and this is my web.config

<system.serviceModel>
    <services>
    <!-- This section is optional with the default configuration introduced
         in .NET Framework 4. -->
        <service name="WCF_Example.Service1"  behaviorConfiguration="mexBehavior">
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:61940/"/>
            </baseAddresses>
          </host>
          <endpoint address="WCF_ServiceExample" binding="basicHttpBinding" contract="WCF_Example.IService1" />
          <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        </service>
    </services>

    <bindings>
      <basicHttpBinding>        
          <binding name="transportSecurity">
            <security mode="TransportWithMessageCredential">
              <message clientCredentialType="UserName" />
            </security>
          </binding>
      </basicHttpBinding>
    </bindings>

    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehavior">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

any help? thanks for the answers.

and this is my contract

[ServiceContract]
public interface IService1
{

    [OperationContract]
    [WebInvoke(UriTemplate = "GetPlayer", ResponseFormat = WebMessageFormat.Json)]
    Player GetPlayer();

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}


Solution 1:[1]

Please enable Diagnostics option by right-clicking on web.config file and select "Edit WCF configuration" menu.

This will create 2 log files in your application path.

Check the "*_web_tracelog.svclog" file, you can find the real error message over there.

Solution 2:[2]

I started to get error 400 all the time a couple of weeks ago while using google "translate" so I tried deleting all cookies, browsing history ETC. for the last MONTH took 2 minutes. -Not very scientific but"Translate" now works again.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pethusamy Venkatachalam
Solution 2 working user