【问题标题】:Error 400 : invalid request on httpwebrequest错误 400:对 httpwebrequest 的请求无效
【发布时间】:2017-08-31 20:51:56
【问题描述】:

我已经构建了一个 WCF 服务,它由 Windows 服务托管在服务器上。一切正常,我已经测试了一些方法。现在我需要像这样从我的客户端发送一个 httprequest:

 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(String.Format(@"http://192.168.0.170:9001/myServ/Start/{0}/{1}", "blabla", "blabla"));
 req.Method = "GET";

问题是我不断在客户端收到异常,指出我的请求无效。我不明白为什么。

这是我的 wcf 服务:

[OperationContract]
[WebInvoke(Method = "GET",
 UriTemplate = "Start/{param1}/{param2}")]
 string Mod(string param1, string param2);

这是我的配置

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true"/>
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding maxBufferSize="2147483647"
                 maxBufferPoolSize="2147483647"
                 maxReceivedMessageSize="2147483647"
                 transferMode="Streamed"
                 sendTimeout="00:05:00"
                 name="WebHttpBinding_InotifyImp">
          <readerQuotas  maxDepth="2147483647"
                         maxStringContentLength="2147483647"
                         maxArrayLength="2147483647"
                         maxBytesPerRead="2147483647"
                         maxNameTableCharCount="2147483647"/>
          <security mode="None" />
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="wcfImp.notifyImp" behaviorConfiguration="NewBehavior">
        <endpoint address="" behaviorConfiguration="DefaultEndPointBehavior" bindingConfiguration="WebHttpBinding_InotifyImp" binding="webHttpBinding" contract="wcfImp.InotifyImp">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9001/notifyImp"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior  name="NewBehavior">
          <!-- To avoid disclosing metadata information, 
          set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="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="True"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="DefaultEndPointBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
  </startup>
</configuration>

【问题讨论】:

  • 您可能还应该传递请求格式。试试这个WCF Parameterised Post Call
  • 我已经发了一个帖子,但没有指定请求格式,它有效。我想知道为什么这个简单的 GET 不能按预期工作。我的问题是我什至无法在调试中访问该服务。我不断在客户端收到此异常
  • 您能帮我们发布一下配置吗?这是一个简单的 GET 请求示例 c-sharpcorner.com/UploadFile/0c1bb2/creating-wcf-rest-service - 如果您还没有访问过该页面。
  • @YawarMurtaza 发布

标签: c# wcf httpwebrequest


【解决方案1】:

我发现了问题。我使用 Visual Studio 2015 在调试模式下托管我的 WCF 服务,以检查我的流程的各个步骤。我不知道为什么,但是虽然我对其他方法没有任何问题,但与 get 方法关联的处理程序有一个无法加载的程序集。我尝试将服务托管在 Windows 服务上,一切正常

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 2020-05-14
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    相关资源
    最近更新 更多