110,535
社区成员
发帖
与我相关
我的任务
分享
http://api.go2map.com/engine/api/geocoder/xml?city=北京市&addr=北京市海淀区中关村东路一号院
<xml>
<response>
<keyword>北京市海淀区中关村东路一号院</keyword>
<match>海淀区中关村东路一号院7号楼威盛大厦</match>
<y>4837046.995794</y>
<city>北京市</city>
<x>1.2949190267684E7</x>
</response>
<status>ok</status>
</xml>
static void Main(string[] args)
{
GetSoGouXML();
}
public static void GetSoGouXML()
{
string ServerPage = "http://api.go2map.com/engine/api/geocoder/";
try
{
ServerPage += "/xml";//WebService中的方法
string strCity = "北京市";//第一个参数
string strAddr = "北京市海淀区中关村东路一号院";//第二个参数
string res = HttpConnectToServer(ServerPage, strCity, strAddr);
}
catch (Exception ex)
{
//
}
}
//发送消息到服务器
public static string HttpConnectToServer(string ServerPage, string strXml, string strData)
{
string postData = "city=" + strXml + "&addr=" + strData;
byte[] dataArray = Encoding.Default.GetBytes(postData);
//创建请求
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage);
request.Method = "POST";
request.ContentLength = dataArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
//创建输入流
Stream dataStream = null;
try
{
dataStream = request.GetRequestStream();
}
catch (Exception)
{
return null;//连接服务器失败
}
//发送请求
dataStream.Write(dataArray, 0, dataArray.Length);
dataStream.Close();
//读取返回消息
string res = string.Empty;
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
res = reader.ReadToEnd();
reader.Close();
}
catch (Exception ex)
{
return null;//连接服务器失败
}
return res;
}