2013-12-19 2 views
0

{"The remote server returned an error: (405) Method Not Allowed}System.InvalidOperationException {System.Net.WebException}WCF POST метод

Web Config:

 <service name="Service" behaviorConfiguration="RestService">  
     <endpoint address="web" binding="webHttpBinding" 
        contract="IService" behaviorConfiguration="ServiceBehavior"> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="RestService"> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="ServiceBehavior"> 
      <webHttp /> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <!--<protocolMapping> 
     <add binding="basicHttpsBinding" scheme="https"/> 
    </protocolMapping>--> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/> 
    </system.serviceModel> 
    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    <directoryBrowse enabled="true"/> 
    </system.webServer> 

Услуги:

public test TestPost(test testPost) 
    { 
     test objtest1 = new test(); 
     objtest1.Address = "Test"; 
     objtest1.Name = "Welcome"; 
     return objtest1; 
    } 


[DataContract] 
public class test 
{ 
    [DataMember(Order = 0)] 
    public string Name { get; set; } 
    [DataMember(Order = 1)] 
    public string Address { get; set; } 
} 


    [OperationContract] 
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     UriTemplate = "TestPost/")] 
    test TestPost(test i); 

Использование Fiddler:

POST /RESTfulService/Service.svc/web/TestPost/ HTTP/1.1 
User-Agent: Fiddler 
Host: localhost:50458 
Content-Length: 43 

HTTP/1.1 400 Bad Request 
Server: ASP.NET Development Server/11.0.0.0 
Date: Thu, 19 Dec 2013 07:19:44 GMT 
X-AspNet-Version: 4.0.30319 
Content-Length: 1647 
Cache-Control: private 
Content-Type: text/html 
Connection: Close 

Метод GET работает нормально, когда я пытаюсь использовать POST метод я получаю ошибку

+0

Показать фактический запрос XML, который вы используете POSTING ... Кажется, проблема в запросе, так как все остальное кажется прекрасным. – Tisho

+0

Я использую формат Json. Формат: { «Имя» = «тест», «Адрес» = «тест» } – user3118128

+0

Операции, я пропустил это. Хорошо, посмотрите на этот вопрос: http://stackoverflow.com/questions/575893/why-does-my-c-sharp-client-posting-to-my-wcf-rest-service-return-400-bad-req , кажется одинаковым. – Tisho

ответ

0

Я попытался выше, например, в моем местном и обнаружили, что вам не хватает один HTTP заголовок с именем

Content-Type: application/json 

Также убедитесь, что вы проходите правильный JSon строку в теле запроса

{"Name" : "test", "Address" : "test"} 

выше трюк поможет вы избавитесь от 400 плохих запросов.

Смежные вопросы