2017-02-01 92 views
0

我有使用WCF和Xamarin這個簡單的POST方法。形式。我嘗試過格式化和發送數據的不同方法,以及服務器端的Web配置,但我仍然收到錯誤的請求錯誤。這是我在客戶端代碼:statuscode 400 reasonphrase'壞請求'1.1版

 OrderContract order = new OrderContract 
     { 
      OrderID = "10550", 
      OrderDate = DateTime.Now.ToString(), 
      ShippedDate = DateTime.Now.ToString(), 
      ShipCountry = "india", 
      OrderTotal = "781" 
     }; 


     HttpClient httpClient = new HttpClient(); 
     var json = JsonConvert.SerializeObject(order); 
     var uri = new Uri(string.Format("http://ipaddress/Services/OrderService.svc/PlaceOrder")); 
     HttpContent httpContent = new StringContent(json); 
     httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
     var result = await httpClient.PostAsync(uri, httpContent); 
     await DisplayAlert("", result.ToString(), "OK"); 

和我在服務器端代碼:

[OperationContract] 
    [WebInvoke(UriTemplate = "/PlaceOrder", 
     BodyStyle = WebMessageBodyStyle.Bare, 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, Method = "POST")] 
    void PlaceOrder(OrderContract order); 

和我的web.config:

<system.serviceModel> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/> 
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <service name="PortalRestService.OrderService"> 
     <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="" contract="PortalRestService.IOrderService" /> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> 
     </service> 
    </services> 

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true"/> 

    </system.serviceModel> 

    <system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

回答

1

請試試這個:

 var s = new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat }; 
     var jsonString = JsonConvert.SerializeObject(jsonObject,s);