日期:2014-05-17 浏览次数:21074 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Windows.Forms;
namespace WindowsFormsApplication11
{
class BaiduApi
{
public static string GetDistance(string rregion, string rcity, string dregion, string dcity)
{
string str = "";
try
{
XDocument docbaidudis = XDocument.Load(Application.StartupPath + "\\baidudis.XML");
int count = int.Parse(creatconxml());
WebRequest request = WebRequest.Create("http://api.map.baidu.com/direction/v1?mode=driving&origin=" + rregion + "&destination=" + dregion + "&origin_region=" + rcity + "&destination_region=" + dcity + "&output=json&ak=" + docbaidudis.Element("sys").Element("key" + count.ToString()).Value);
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("gb2312"));
str = reader.ReadToEnd();
reader.Close();
reader.Dispose();
response.Close();
int statusindex = str.IndexOf("status");
string status = str.Substring(statusindex + 8, 1);