Bad Request when making a call to a REST WCF WebService

爷,独闯天下 提交于 2019-12-25 04:19:59

问题


I'm writing an Android application and a WCF REST service to call from it. When I try to make a call to a certain method I receive an "Bad Request" message. In a previous test I managed to do this call and its worked, but after some changes in classes I could not again.

This is the classes in the service:

public class Venda
    {
        public int Cod { get; set; }
        public string Pedido { get; set; }
        public int Cliente { get; set; }
        public int Vendedor { get; set; }
        public string Modo { get; set; }
        public DateTime Data { get; set; }
        public double Total { get; set; }
        public string Observacoes { get; set; }
        public string TipoPagto { get; set; }
        public double Desconto { get; set; }
        public int ST { get; set; }
        public int GF { get; set; }

        public List<DetalheVenda> DetalhesVenda { get; set; }

        public Venda()
        {
            DetalhesVenda = new List<DetalheVenda>();
        }
    }

public class DetalheVenda
    {
        public int Cod { get; set; }
        public string Pedido { get; set; }
        public string Produto { get; set; }
        public double Quantidade { get; set; }
        public double ValorVenda { get; set; }
        public double ValorCompra { get; set; }
        public double ValorVendaMinimo { get; set; }
        public int ST { get; set; }
        public double Desconto { get; set; }
        public int Vendedor { get; set; }
        public int Cliente { get; set; }
        public string Modo { get; set; }
        public DateTime Data { get; set; }
        public string Grade { get; set; }
        public string SubGrade { get; set; }
    }

Method interface and implementation:

[OperationContract]
        [WebInvoke(
            Method = "POST",
            UriTemplate = "AdicionaVenda",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        void AdicionaVenda(Venda venda);


 //a breakingpoint nor enter here
public void AdicionaVenda(Venda venda)
        {
            repositorio.AdicionaVenda(venda);
        }

This is the JSON that I send as the body request: {"venda": {"Cod":null,"Pedido":"1.1.56","Cliente":0,"Vendedor":1,"Modo":"A prazo","Data":"2014-02-22","Total":0,"Observacoes":"","TipoPagto":"Tipo Teste","Desconto":0,"ST":1,"GF":0, "DetalhesVenda":[{"Cod":null,"Pedido":"1.1.56","Produto":"13","Quantidade":0,"ValorVenda":9,"ValorCompra":5,"ValorVendaMinimo":0,"ST":0,"Desconto":0,"Vendedor":0,"Cliente":0,"Modo":"A prazo","Data":"2014-02-22","Grade":"grade 1","SubGrade":"subgrade 1"}]}}


回答1:


Actually its not a problem with DateTime filed in your model classes but it is something from .NET.

There is no standard JSON format of Date.This is why you are facing this problem. There are two solutions for this:

  1. Use JSON.NET.

  2. Or, set the type of your date to String in model class and later on when you receive json, parse it into DateTime.

See these links:

The Right JSON Date Format and Incompatible date format in JSON from response.




回答2:


I discover that problem is the DateTime new fields in model classes of service.



来源:https://stackoverflow.com/questions/21960682/bad-request-when-making-a-call-to-a-rest-wcf-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!