ashx

请求URL 获取返回数据(JSON格式) 解析JSON格式数据   获取IP地址及城市名称(搜狐)
  HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://pv.sohu.com/cityjson?ie=utf-8");//创建一个URL请求
  HttpWebResponse response = (HttpWebResponse)request.GetResponse();//获取响应,即发送请求 
  Stream responseStream = response.GetResponseStream();
  StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
  //string json = streamReader.ReadToEnd();//获取返回数据(JSON格式)
  string json = streamReader.ReadToEnd().Substring(18);//截取保留18位后的数据
  string JSONS = json.Remove(json.LastIndexOf(";"), 1);//移除最后一个字符
  JavaScriptSerializer js = new JavaScriptSerializer();//序列化
  Dictionary<string, object> dic = js.Deserialize<Dictionary<string, object>>(JSONS);//cip ip地址 cname 地址城市 示例:dic["cip"]
//连接ACCESS数据库插入数据
  String sqlconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = E:/项目/流量报表/XMLS2013/baobiao/data/enterprise.access.config";
  System.Data.OleDb.OleDbConnection myConnection = new System.Data.OleDb.OleDbConnection(sqlconn); 
  myConnection.Open();//打开连接
  System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand("insert into stHour(Ip,UV,FirstDate) values('"+dic["cip"]+"','222','2018/10/15 14:35:15')", myConnection);
  System.Data.OleDb.OleDbDataReader myReader;
  myReader = myCommand.ExecuteReader();
  myConnection.Close();//关闭连接
//存session
context.Session["name"] = 123;
//取session
string name = context.Session["name"].ToString();

 

posted @ 2018-10-15 14:01  时光博客  阅读(361)  评论(0编辑  收藏  举报