在Xamarin.iOS中返回JSON
我试图运行一个示例WebRequest来返回JSON数据。我不断得到XML响应。知道为什么吗在Xamarin.iOS中返回JSON,xamarin.ios,xamarin,xamarin-studio,Xamarin.ios,Xamarin,Xamarin Studio,我试图运行一个示例WebRequest来返回JSON数据。我不断得到XML响应。知道为什么吗 this.btnGetCoordinates.TouchUpInside+=(发送方,e)=>{ var rxcu=“198440”; var request=HttpWebRequest.Create(string.Format)(@)http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/{0}/allinfo“,rxcui)); request.Method=“GE
this.btnGetCoordinates.TouchUpInside+=(发送方,e)=>{
var rxcu=“198440”;
var request=HttpWebRequest.Create(string.Format)(@)http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/{0}/allinfo“,rxcui));
request.Method=“GET”;
request.ContentType=“application/json”;
BeginGetResponse(新的AsyncCallback(ProcessGetCoordinates),request);
};
返回true;
}
void ProcessGetCoordinates(IAsyncResult iar){
HttpWebRequest请求=(HttpWebRequest)iar.AsyncState;
使用(HttpWebResponse=(HttpWebResponse)request.EndGetResponse(iar)){
if(response.StatusCode!=HttpStatusCode.OK){
Console.Out.WriteLine(“获取数据时出错。服务器返回状态代码:{0}”,response.StatusCode);
}
使用(StreamReader strm=newstreamreader(response.GetResponseStream())){
string content=strm.ReadToEnd();
if(string.IsNullOrWhiteSpace(content)){
Console.Out.WriteLine(“响应包含空体…”);
}否则{
Console.Out.WriteLine(“响应正文:\r\n{0}”,内容);
}
}
}
}
您需要将请求的Accept头设置为“application/json”
var request=HttpWebRequest.Create(string.Format)(@)http://rxnav.nlm.nih.gov/REST/RxTerms/rxcui/{0}/allinfo“,rxcui));
request.Method=“GET”;
request.Accept=“application/json”;