0
我已經構建了由Windows服務託管在服務器上的WCF服務。一切工作正常,我已經測試了一些方法。現在,我需要從我的客戶端發送一個HttpRequest像這樣的:錯誤400:httpwebrequest上的無效請求
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](https://stackoverflow.com/questions/25830289/how-to-pass-parameters-to-a-wcf-post-method-a-restful-services) –
我已經做了一個發佈而不指定請求格式,並且工作。我想知道爲什麼這個簡單的GET沒有按預期工作。我的問題是,我甚至無法在調試中達到服務。我一直在客戶端得到這個異常 –
你能爲我們發佈配置嗎?這裏是一個simle GET請求示例http://www.c-sharpcorner.com/UploadFile/0c1bb2/creating-wcf-rest-service/ - 如果你沒有去過那個頁面。 –