![](/img/trans.png)
[英](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.