public frmApiEdit(int id = 0) { InitializeComponent(); _lit = new xl_rp.Sqlite(); listDb.DisplayMember = "name"; listUser.DisplayMember = "name"; listRight.DisplayMember = "name"; dgParam.ColumnCount = 4; dgParam.ColumnHeadersVisible = true; dgParam.Columns[0].DataPropertyName = "name"; dgParam.Columns[0].HeaderText = "名称"; dgParam.Columns[1].DataPropertyName = "key"; dgParam.Columns[1].HeaderText = "sql标记"; dgParam.Columns[2].DataPropertyName = "defValue"; dgParam.Columns[2].HeaderText = "默认值"; dgParam.Columns[3].DataPropertyName = "note"; dgParam.Columns[3].HeaderText = "备注"; if (id == 0) { _apiObj = new Entity.ApiObj(); this.Text = "新增"; } else { _apiObj = ApiObj.Load(_lit, id); this.Text = _apiObj.name; } txtName.DataBindings.Add("Text", _apiObj, "name"); txtNumber.DataBindings.Add("Text", _apiObj, "number"); txtNote.DataBindings.Add("Text", _apiObj, "note"); dgParam.DataSource = _apiObj.param; listRight.DataSource = _apiObj.right; listDb.DataSource = Db.Load(_lit); List <User> users = User.Load(_lit); for (int i = users.Count - 1; i >= 0; --i) { for (int j = 0; j < _apiObj.right.Count; j++) { if (_apiObj.right[j].id == users[i].id) { users.RemoveAt(i); } } } listUser.DataSource = users; }
private void handleClient(NetworkStream stream) { //byte[] question = Encoding.UTF8.GetBytes("\r\n(Jeśli chcesz wyjść wpisz exit)" + //"\r\nPodaj lokalizację, aby sprawdzić warunki pogodowe: "); byte[] localization = new byte[64]; while (true) { //stream.Write(question, 0, question.Length); int localizationLength = stream.Read(localization, 0, 64); //byte[] trash = new byte[2]; //stream.Read(trash, 0, 2); string city = Encoding.UTF8.GetString(localization, 0, localizationLength); Console.Write(city); //if (city == "exit") //{ // byte[] bye = Encoding.UTF8.GetBytes("Rozłączono z serwerem"); // stream.Write(bye, 0, bye.Length); // break; //} try { ApiObj cityWeather = WeatherGetter.getWeather(city); string outputValues = string.Format("{0}|{1}|{2}|{3}|{4}|{5}|{6}", cityWeather.coord.lon, cityWeather.coord.lat, cityWeather.main.temp, cityWeather.main.pressure, cityWeather.main.humidity, cityWeather.wind.speed, cityWeather.sys.country); byte[] weather = Encoding.UTF8.GetBytes(outputValues); stream.Write(weather, 0, weather.Length); //byte[] licence = Encoding.UTF8.GetBytes("Dane pochodzą z openweathermap.org"); //stream.Write(licence, 0, licence.Length); Console.WriteLine("Zwrócono poprawnie dane!"); } catch (NullReferenceException e) { Console.WriteLine(e.Message); byte[] error = Encoding.UTF8.GetBytes("Wpisano niepoprawną miejscowość!\r\n"); stream.Write(error, 0, error.Length); } } }
public async Task <Response> GetCurrencyAsync(string urlBase, string Fecha, string Now, string Base) { try { HttpClient client = new HttpClient { BaseAddress = new Uri(urlBase), }; string url = $"history?start_at={Fecha}&end_at={Now}&base={Base}"; //espacios en string HttpResponseMessage response = await client.GetAsync(url); string result = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(new Response { IsSuccess = false, Message = result, }); } ApiObj model = JsonConvert.DeserializeObject <ApiObj>(result); return(new Response { IsSuccess = true, Result = model }); } catch (Exception ex) { return(new Response { IsSuccess = false, Message = ex.Message }); } }
public override void OnActionExecuting(HttpActionContext filterContext) { ApiObj response; try { //StationId var stationIdValue = filterContext.Request.Headers.GetValues("StationId"); //EnrollerRegId var enrollerRegIdValue = filterContext.Request.Headers.GetValues("EnrollerRegId"); //ApiAccessKey var apiKeyValue = filterContext.Request.Headers.GetValues("APIAccessKey"); //StationName var stationNameValue = filterContext.Request.Headers.GetValues("StationName"); if (stationIdValue == null || apiKeyValue == null || enrollerRegIdValue == null || stationNameValue == null) { response = new ApiObj { IsSuccessful = false, ErrorMessage = "Empty / Invalid Authentication Parameter Was Encountered", StationInfo = new UploadClientStation() }; filterContext.Request.Properties.Add("AuthResponse", response); return; } var apiStationId = stationIdValue.FirstOrDefault(); var stationKey = apiKeyValue.FirstOrDefault(); var enrollerRegId = enrollerRegIdValue.FirstOrDefault(); var stationName = stationNameValue.FirstOrDefault(); if (string.IsNullOrEmpty(apiStationId) || string.IsNullOrEmpty(stationKey) || string.IsNullOrEmpty(stationName)) { response = new ApiObj { IsSuccessful = false, ErrorMessage = "Empty / Invalid Authentication Parameter Was Encountered", StationInfo = new UploadClientStation() }; filterContext.Request.Properties.Add("AuthResponse", response); return; } string msg; //var stationInfo = ServiceProvider.Instance() // .GetClientStationServices() // .ValidateClientAccess(apiStationId, stationName, stationKey, out msg); var stationInfo = ServiceProvider.Instance() .GetClientStationServices() .ValidateUploadClientAccess(apiStationId, stationName, stationKey, out msg); if (stationInfo == null || stationInfo.ClientStationId < 1) { response = new ApiObj { IsSuccessful = false, ErrorMessage = "Unable to authenticate request", StationInfo = new UploadClientStation() }; filterContext.Request.Properties.Add("AuthResponse", response); return; } var enrollerInfo = ServiceProvider.Instance() .GetEnrollerServices() .GetEnrollerByRegId(enrollerRegId); if (enrollerInfo == null || enrollerInfo.EnrollerId < 1) { response = new ApiObj { IsSuccessful = false, ErrorMessage = "Unable to authenticate request! Operator does not exist", StationInfo = new UploadClientStation() }; filterContext.Request.Properties.Add("AuthResponse", response); return; } stationInfo.EnrollerRegId = enrollerInfo.EnrollerRegId; stationInfo.EnrollerId = enrollerInfo.EnrollerId; response = new ApiObj { IsSuccessful = true, StationInfo = stationInfo }; filterContext.Request.Properties.Add("AuthResponse", response); } catch (Exception ex) { response = new ApiObj { IsSuccessful = false, ErrorMessage = "Authentication Failed!", TechErrorMessage = ex.Message, StationInfo = new UploadClientStation() }; filterContext.Request.Properties.Add("AuthResponse", response); return; } base.OnActionExecuting(filterContext); }
private void Srv_requestHandler(HttpListenerContext ctx) { try { List <string> sl = new List <string>(); if (txtInfo.Text != "") { sl = new List <string>(txtInfo.Lines); } string info = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + ctx.Request.Url.OriginalString; if (sl.Count >= 1000) { sl.RemoveAt(sl.Count - 1); } sl.Insert(0, info); txtInfo.Lines = sl.ToArray(); string route = ctx.Request.RawUrl.Split('?')[0]; ApiObj ao = ApiObj.Load(new xl_rp.Sqlite(), route); if (ao == null) { ctx.Response.StatusCode = 404; ctx.Response.StatusDescription = "Url Not Found"; ctx.Response.Close(); return; } ctx.Response.StatusCode = 200; //接收Get参数 string type = ctx.Request.QueryString["type"]; string userId = ctx.Request.QueryString["userId"]; string password = ctx.Request.QueryString["password"]; string filename = Path.GetFileName(ctx.Request.RawUrl); string userName = "******";//HttpUtility.ParseQueryString(filename).Get("userName");//避免中文乱码 Dictionary <string, string> param = new Dictionary <string, string>(); foreach (string k in ctx.Request.QueryString.AllKeys) { param.Add(k, ctx.Request.QueryString[k]); } //进行处理 //Console.WriteLine("收到数据:" + userName); //接收POST参数 Stream stream = ctx.Request.InputStream; System.IO.StreamReader reader = new System.IO.StreamReader(stream, Encoding.UTF8); String body = reader.ReadToEnd(); //Console.WriteLine("收到POST数据:" + HttpUtility.UrlDecode(body)); //Console.WriteLine("解析:" + HttpUtility.ParseQueryString(body).Get("userName")); //使用Writer输出http响应代码,UTF8格式 using (StreamWriter writer = new StreamWriter(ctx.Response.OutputStream, Encoding.UTF8)) { string rst = Newtonsoft.Json.JsonConvert.SerializeObject(ao.Exec(param)); writer.Write(rst); writer.Close(); ctx.Response.Close(); } }catch (Exception ex) { MessageBox.Show(ex.Message); } }