求帮忙将一个c#项目转dephi 用baidu的api勉强写个查询两个地点之间的驾车距离。但现在需要改成dephi的。以前从来没写个dephi。现在项目有很着急,求大虾们帮帮忙 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 项目地址http://download.csdn.net/download/cmy_ymc/5990727 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); if (status == "0") { int lastdisindex = str.LastIndexOf("distance"); string distance = str.Substring(lastdisindex + 10, str.IndexOf(",", lastdisindex) - lastdisindex); return distance.Remove(distance.IndexOf(',')); } else if (status == "2") { return status; } else//(status == "5") { XDocument docbaiducount = XDocument.Load(Application.StartupPath + "\\baiducount.XML"); docbaiducount.Element("sys").Element("key").Value = (int.Parse(docbaiducount.Element("sys").Element("key").Value) + 1).ToString(); docbaiducount.Save("baiducount.xml"); return GetDistance(rregion, rcity, dregion, dcity); } } catch (Exception tt) { return "0"; } } private static string creatconxml() { if (!File.Exists("baiducount.xml")) { XmlWriter writer = null; try { writer = XmlWriter.Create("baiducount.xml"); writer.WriteStartElement("sys"); writer.WriteElementString("date", System.DateTime.Today.ToShortDateString()); writer.WriteElementString("key", "1"); writer.WriteEndDocument(); writer.Flush(); } finally { if (writer != null) writer.Close(); } return "1"; } else { XDocument doc = XDocument.Load(Application.StartupPath + "\\baiducount.XML"); string datetime = doc.Element("sys").Element("date").Value; if (Convert.ToDateTime(datetime).Date != System.DateTime.Today) { doc.Element("sys").Element("date").Value = System.DateTime.Today.ToShortDateString(); doc.Element("sys").Element("key").Value = "1"; doc.Save("baiducount.XML"); return "1"; } else { return doc.Element("sys").Element("key").Value; } } } }} <?xml version="1.0" encoding="utf-8"?><sys> <date>2013-09-03</date> <key>1</key></sys><?xml version="1.0" encoding="utf-8"?><sys> <key1>7bf97874e7486e04b8556eab41117713</key1> <key2>A8a4c484282b5c902f4800a3b732c3bd</key2> <key3>D18b1faa880ba25c96044f2bbacbfc52</key3></sys>因为百度的key是有使用限制的,一个key一天只能用1000次两个xml文件是用来控制key的使用的。当一个key使用超过1000,就会转到下个key 对于无法显示网页的问题 高难都问题,百度都找不到答案 无法在web服务器上启动调试. Web服务器的配置不正确 VS2008 在线等 两个gridview放在同一页面,绑定的数据源是同一数据库的同一张表,怎么绑?绑定的sql语句条件不同而已 WinForm问题关于MDI和坐标请高手解答 用C#代码实现窗口切换时出现的问题 请教 C#内存释放的问题。 100分拜求 如何批量命名 我的vs2010的提示功能消失了,求指导 配方问题如何计算 怎样把照片的相对地址存入orcal数据库,再用VS调用?
http://download.csdn.net/download/cmy_ymc/5990727
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);
if (status == "0")
{
int lastdisindex = str.LastIndexOf("distance");
string distance = str.Substring(lastdisindex + 10, str.IndexOf(",", lastdisindex) - lastdisindex);
return distance.Remove(distance.IndexOf(','));
}
else if (status == "2")
{
return status;
}
else//(status == "5")
{
XDocument docbaiducount = XDocument.Load(Application.StartupPath + "\\baiducount.XML");
docbaiducount.Element("sys").Element("key").Value = (int.Parse(docbaiducount.Element("sys").Element("key").Value) + 1).ToString();
docbaiducount.Save("baiducount.xml");
return GetDistance(rregion, rcity, dregion, dcity);
}
}
catch (Exception tt)
{
return "0";
}
}
private static string creatconxml()
{
if (!File.Exists("baiducount.xml"))
{
XmlWriter writer = null;
try
{
writer = XmlWriter.Create("baiducount.xml");
writer.WriteStartElement("sys");
writer.WriteElementString("date", System.DateTime.Today.ToShortDateString());
writer.WriteElementString("key", "1");
writer.WriteEndDocument();
writer.Flush();
}
finally
{
if (writer != null)
writer.Close();
}
return "1";
}
else
{
XDocument doc = XDocument.Load(Application.StartupPath + "\\baiducount.XML");
string datetime = doc.Element("sys").Element("date").Value;
if (Convert.ToDateTime(datetime).Date != System.DateTime.Today)
{
doc.Element("sys").Element("date").Value = System.DateTime.Today.ToShortDateString();
doc.Element("sys").Element("key").Value = "1";
doc.Save("baiducount.XML");
return "1";
}
else
{
return doc.Element("sys").Element("key").Value;
}
}
}
}
}
<sys>
<date>2013-09-03</date>
<key>1</key>
</sys>
<?xml version="1.0" encoding="utf-8"?>
<sys>
<key1>7bf97874e7486e04b8556eab41117713</key1>
<key2>A8a4c484282b5c902f4800a3b732c3bd</key2>
<key3>D18b1faa880ba25c96044f2bbacbfc52</key3>
</sys>因为百度的key是有使用限制的,一个key一天只能用1000次
两个xml文件是用来控制key的使用的。当一个key使用超过1000,就会转到下个key