[英](400) Bad Request -> The remote server returned an error: (400) Bad Request
[英]HOW TO FIX: The remote server returned an error: (400) Bad Request
我正在尝试修复此错误,我知道还有其他类似的问题也有相同的错误,但行: 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
}
请通过右键单击 web.config 文件并选择“编辑 WCF 配置”菜单来启用诊断选项。
这将在您的应用程序路径中创建 2 个日志文件。
检查“*_web_tracelog.svclog”文件,你可以在那里找到真正的错误信息。
几周前我开始在使用谷歌“翻译”时一直收到错误 400,所以我尝试删除所有 cookie、浏览历史记录等。 上个月花了 2 分钟。 - 不是很科学,但“翻译”现在又可以工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.