Bad Request when making a call to a REST WCF WebSe

2019-09-08 17:00发布

问题:

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.