如何修复:远程服务器返回错误:(400)错误请求

时间:2016-06-30 18:59:03

标签: c# asp.net xml wcf asp.net-mvc-4

我试图修复此错误,我知道还有其他类似的问题出现同样的错误,但是行request.ContentLength = 0;它没有修复我的异常。

这是我的图片代码:

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);
}

,这是我的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>

任何帮助?谢谢你的回答。

这是我的合同

[ServiceContract]
public interface IService1
{

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

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

    // TODO: Add your service operations here
}

2 个答案:

答案 0 :(得分:0)

请右键单击web.config文件并选择&#34;编辑WCF配置&#34;启用诊断选项。菜单。

这将在您的应用程序路径中创建2个日志文件。

检查&#34; * _ web_tracelog.svclog&#34;文件,你可以在那里找到真正的错误信息。

答案 1 :(得分:0)

我几周前开始一直使用谷歌“翻译”时出现错误400,所以我尝试删除所有的cookie,浏览历史记录ETC.最后一个月花了2分钟。 - 非常科学,但“翻译”现在再次有效。