【发布时间】: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