dr_twilight

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

<script type="text/javascript">
$(function () {
$("#btnOK").click(function () {
$.ajax({
type: "POST",
url: "/Storage/CreateGeotable",
data: { name: "位置数据表", geotype: 1, is_published: 1, ak: "DqsjAlqpyHfX2igOzIxCeFwU" },
datatype: "json",
success: function (data) {
alert(data.length);
},
error: function () {
alert("error");
}
});
});
//
$("#btnGet").click(function () {
$.ajax({
type: "POST",
url: "/Storage/HttpGet",
data: { url: "http://api.map.baidu.com/geodata/v3/geotable/list", ak: "DqsjAlqpyHfX2igOzIxCeFwU" },
datatype: "json",
success: function (data) {
alert(data.length);
},
error: function () {
alert("error");
}
});
});
});
</script>

 

public JsonResult HttpPost(string name, int geotype, int is_published, string ak)
{
string url = "http://api.map.baidu.com/geodata/v3/geotable/create";
string postData = "name=" + name + "&geotype=" + geotype + "&is_published=" + is_published + "&ak=" + ak + ""; //
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(url);
objWebRequest.Method = "POST";
objWebRequest.ContentType = "application/x-www-form-urlencoded";
objWebRequest.ContentLength = byteArray.Length;
Stream newStream = objWebRequest.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);
newStream.Close();

HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
string textResponse = sr.ReadToEnd(); // 返回的数据
return Json(url);
}

public string HttpGet(string url, string ak)
{
string getUrl = url + "?ak=" + ak;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(getUrl);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();

return retString;
}

posted on 2015-07-02 16:21  dr_twilight  阅读(101)  评论(0编辑  收藏  举报