WCF Rest服务获得IOS和Andriod消耗的JSon格式问题 [英] WCF Rest Service getting JSon Format issue consumed by IOS, and Andriod
问题描述
WCF测试客户端在IOS和Android中作为转义序列Json格式获得响应。
{\BusID \:\1 \,\Latitude \:\23 \,\经度\:\ 34 \,\TrackTime \:\\}
但它显示以下格式的错误
{BusID:1,纬度:23,经度:34,TrackTime: } ..
显示以下错误
Hi,
WCF Test Client is getting response as escape sequence Json Format in IOS and Android.
"{\"BusID\":\"1\",\"Latitude\":\"23\",\"Longitude\":\"34\",\"TrackTime\":\"\"}"
But It is showing error for below format
{"BusID":"1","Latitude":"23","Longitude":"34","TrackTime":""}..
Showing below error
Status
400 Bad Request Show explanation Loading time: 15
Request headers
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo
Content-Type: application/json
Accept: */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Response headers
Cache-Control: private
Content-Length: 1780
Content-Type: text/html
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 23 Apr 2014 04:09:47 GMT
我的WCF示例代码
my WCF Sample code
[OperationContract]
[WebInvoke(UriTemplate = "UpdateTrackDetails",
Method = "POST")]
int UpdateTrackDetails(string trackdetails);
//
//
public int UpdateTrackDetails(string trackdetails)
{
int result = 0;
TrackDetails _BusTrack = JsonConvert.DeserializeObject<TrackDetails>(trackdetails);
using (var context = new SchoolBusTrackEntities())
{
context.BusTrackDetails.Add(
new BusTrackDetail
{
BusID = Convert.ToInt32(_BusTrack.BusID),
Latitude = _BusTrack.Latitude,
Longitude = _BusTrack.Longitude,
TrackTime =DateTime.Now,
}
);
result = context.SaveChanges();
}
return result;
}
我在这里使用HttpClient。问题我主要是通过IOS和Android应用程序获取WCF服务。
我的样本wcf测试客户端
static async Task Post(字符串BusID,字符串纬度,字符串经度,字符串TrackTime)
{
使用(HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(http://192.168.1.42/BusTrackService/);
client.DefaultRequestHeaders.Accept.Add(
新的System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(application / json));
BusTrack1 _Bus = new BusTrack1();
_Bus.BusID = BusID;
_Bus.Latitude =纬度;
_Bus.Longitude =经度;
_Bus.TrackTime = TrackTime;
字符串数据= JsonConvert.SerializeObject(_Bus); // {busid:12}
HttpResponseMessage响应= client.PostAsJsonAsync(SchoolBusTrack.svc / UpdateTrackDetails,数据).Result;
if(response.IsSuccessStatusCode)
{
var product = await response.Content.ReadAsAsync< string>();
}
}
}
提前致谢...
I am using here HttpClient. Issue I am mainly getting for WCF service consuming by IOS and Android applications.
My sample wcf test client
static async Task Post(string BusID, string Latitude, string Longitude, string TrackTime)
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("http://192.168.1.42/BusTrackService/");
client.DefaultRequestHeaders.Accept.Add(
new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
BusTrack1 _Bus = new BusTrack1();
_Bus.BusID = BusID;
_Bus.Latitude = Latitude;
_Bus.Longitude = Longitude;
_Bus.TrackTime = TrackTime;
string data = JsonConvert.SerializeObject(_Bus);//{"busid:12"}
HttpResponseMessage response = client.PostAsJsonAsync("SchoolBusTrack.svc/UpdateTrackDetails", data).Result;
if (response.IsSuccessStatusCode)
{
var product = await response.Content.ReadAsAsync<string>();
}
}
}
Thanks in advance...
推荐答案
发送时尝试在客户端上将内容类型设置为application / json。
Try setting the content type to "application/json" on your clients when sending.
这篇关于WCF Rest服务获得IOS和Andriod消耗的JSon格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!