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

问题描述 投票:0回答:2

我正在尝试修复此错误,我知道还有其他类似的问题具有相同的错误,但是该行:

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
}
c# asp.net xml wcf asp.net-mvc-4
2个回答
0
投票

请右键单击 web.config 文件并选择“编辑 WCF 配置”菜单来启用诊断选项。

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

查看“*_web_tracelog.svclog”文件,你可以在那里找到真正的错误信息。


0
投票

几周前我在使用谷歌“翻译”时开始一直收到错误 400,所以我尝试删除所有 cookie、浏览历史记录等。上个月花了 2 分钟。 - 不太科学,但“翻译”现在又可以工作了。

© www.soinside.com 2019 - 2024. All rights reserved.