【问题标题】:HOW TO FIX: The remote server returned an error: (400) Bad Request如何修复:远程服务器返回错误:(400)错误请求
【发布时间】:2016-11-03 00:42:42
【问题描述】:

我正在尝试修复这个错误,我知道还有另一个类似的问题也有同样的错误,但是这行: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
}

【问题讨论】:

  • The remote server returned an error: (400) Bad Request 这可能有很多原因。如果您可以截取错误页面的屏幕截图并添加到 OP,那就更好了。根据现在,我的猜测可能与您的 localhost 应用程序的身份验证有关。

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      相关资源
      最近更新 更多