.net调用api获取json或者xml,Get
·
public static string HttpApi(string url, string jsonstr, string type)
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
request.Headers.Add("command:");
request.Headers.Add("token:");
request.Accept = "text/html,application/xhtml+xml,*/*";
request.ContentType = "application/json";
request.Method = type.ToUpper().ToString();//get
//byte[] buffer = encoding.GetBytes(jsonstr);
//request.ContentLength = buffer.Length;
//request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return reader.ReadToEnd();
}
}
//xml
public static string HttpXml(string url, string jsonstr, string type)
{
Encoding encoding = Encoding.UTF8;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址
request.Accept = "text/html,application/xhtml+xml,*/*";
request.ContentType = "application/json";
request.Method = type.ToUpper().ToString();//get
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
XmlDocument document = new XmlDocument();
XmlReader xmlreader = XmlReader.Create(reader);
document.Load(xmlreader);
XmlNode node = document.SelectSingleNode("response/route/paths/path");
return node.SelectSingleNode("distance").InnerText;
}
}
更多推荐




所有评论(0)