做一个REST WCF WebService的电话时坏请求 [英] Bad Request when making a call to a REST WCF WebService
问题描述
我写一个Android应用程序和WCF REST服务从中调用。当我尝试拨打电话,以一定的方法我收到一个错误的请求的消息。在previous测试我设法做到这一点调用及其工作,但在课堂上的一些变化后,我不能试。
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.
这是在服务类:
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);
}
这是我送的身体要求JSON:
{文达:
{鳕鱼:空,Pedido:1.1.56,Cliente:0,Vendedor:1,摩多:A prazo,数据:2014年2月22日 总计0,Observacoes:,TipoPagto:TIPO泰斯特,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,数据:2014年2月22日,等级:1级,路基:路基1}]}}
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"}]}}
推荐答案
其实它不是一个以的DateTime
问题在你的模型类申请,但它是从.NET的东西。
Actually its not a problem with DateTime
filed in your model classes but it is something from .NET.
有 。这没有标准的JSON格式。这就是为什么你面对这个问题。有两个方案来解决这个:
There is no standard JSON format of Date.This is why you are facing this problem. There are two solutions for this:
-
使用 JSON.NET 。
或者,在模型类设置日期类型字符串
后来当您收到JSON,解析成日期时间上
。
Or, set the type of your date to String
in model class and later on when you receive json, parse it into DateTime
.
请参阅以下链接:
<一个href=\"http://stackoverflow.com/questions/14159097/wcf-client-how-can-i-deserialize-an-incompatible-date-format-from-the-json-respo\">The右JSON日期格式和<一个href=\"http://stackoverflow.com/questions/14159097/wcf-client-how-can-i-deserialize-an-incompatible-date-format-from-the-json-respo\">Incompatible在JSON日期格式从响应。
这篇关于做一个REST WCF WebService的电话时坏请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!