WCF Post操作会遇到400个错误请求 - 本地主机IIS 7.5上的主机名无效

时间:2012-12-22 10:10:02

标签: c# wcf post

我可以使用一些帮助,因为我已经被困了2天了。我用谷歌搜索,但未能找到正确的解决方案。

任何人都可以帮忙解决问题,或者如何更好地调试?

发送给localhost的内容

POST /Service1.svc/addt HTTP/1.1
Content-Type: application/json
Host: localhost.:13309
Content-Length: 25 
Expect: 100-continue
Connection: Keep-Alive

回应(与小提琴手一起观看)

> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Hostname</h2>
<hr><p>HTTP Error 400. The request hostname is invalid.</p>
</BODY></HTML>

GET操作正常,所以我觉得POST操作确实是错误的。

有关Web.config和Post操作

下面的信息
string requestData = "{\"ID\":1,\"Make\":\"Ferrari\"}";
byte[] data = Encoding.UTF8.GetBytes(requestData);
request = (HttpWebRequest)WebRequest.Create("http://localhost.:13309/Service1.svc/addt");
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
WebResponse webr = request.GetResponse();




<system.web>
    <compilation debug="true" targetFramework="4.5" />
    <customErrors mode="Off"/>
    <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,:,\,?" maxRequestLength="2097151" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="FinanceService.Service1" behaviorConfiguration="ServiceBehaviour">
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="webHttpBinding" contract="FinanceService.IService1" behaviorConfiguration="web">
          <!-- 
              Upon deployment, the following identity element should be removed or replaced to reflect the 
              identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
              automatically.
          -->
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

3 个答案:

答案 0 :(得分:2)

自己解决 - 从教程中的文件开始,让这些文件工作,然后逐步进行更改,直到我得到所需的结果。

赞美这个Tutorial

结合WCF Post查看400个不良请求中的大量项目,这里有一些指示。

  1. Web.Config似乎不需要太多关注。如果它适用于GET,则很可能已正确配置POST操作

  2. 您要发布的正文/数据格式非常烦躁!如果您声称它是JSON,请确保它是正确的JSON。我(天真)的印象是,无论我放入身体的是什么,都会出现在另一端,但服务员会检查,并返回“不良请求”。

答案 1 :(得分:1)

您正在询问如何更好地进行调试。我强烈建议不要添加。在localhost之后。在这种情况下,你很可能会遇到另一个错误。

请参阅“Why don't I see traffic sent to http://localhost or http://127.0.0.1

  

最简单的解决方法是使用您的计算机名称作为主机名而不是Localhost或127.0.0.1。因此,举例来说,不要点击http:// localhost :8081 / mytestpage.aspx,而是访问http:// machinename :8081 / mytestpage.aspx。

希望能帮助您捕获与使用POST方法相关的真实错误。

答案 2 :(得分:0)

如果您使用NInject激活它,则应使用NinjectServiceHostFactory。