/// <summary> /// 传数据 /// </summary> /// <param name="jsonContent"></param> /// <returns></returns> public string PostForSql(object dataobj) { string jsonContent = JsonController.SerializeToJson(dataobj); WebRequest request = (WebRequest)HttpWebRequest.Create(_urlService); request.Method = "POST"; byte[] postBytes = null; request.ContentType = @"application/x-www-form-urlencoded"; postBytes = Encoding.UTF8.GetBytes(jsonContent); request.ContentLength = postBytes.Length; request.Timeout = 3000; using (Stream outstream = request.GetRequestStream()) { outstream.Write(postBytes, 0, postBytes.Length); } string result = string.Empty; using (WebResponse response = request.GetResponse()) { if (response != null) { using (Stream stream = response.GetResponseStream()) { using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } } } } return(result); }
/// <summary> /// 传数据 /// </summary> /// <param name="jsonContent">类实体</param> /// <returns></returns> public async Task <string> Post(object jsonContent) { string postContent = JsonController.SerializeToJson(jsonContent); var httpClient = new HttpClient(); var response = await httpClient.PostAsync(_urlService, new StringContent(postContent)); response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); //return await Task.Run(() => JsonController.DeSerializeToObject(content)); return(content); }
/// <summary> /// sql请求 /// </summary> /// <param name="pageId"></param> public SqlExcuteReturn ExcuteSqlWithReturn(string sql) { string apitype = JsonApiType.execute; BaseRequest bj = GetCommonBaseRequest(apitype); string type = "db"; SqlExcuteRequest sr = new SqlExcuteRequest(sql, type); bj.data = sr; try { var result = this.PostForSql(bj); BaseReturn brj = JsonController.DeSerializeToClass <BaseReturn>(result); try { SqlExcuteReturn ser = JsonController.DeSerializeToClass <SqlExcuteReturn>(brj.data.ToString()); if (ser.error_code.Equals("1")) { ser.data = null; } return(ser); } catch { SqlExcuteReturnError ser = JsonController.DeSerializeToClass <SqlExcuteReturnError>(brj.data.ToString()); SqlExcuteReturn serr = new SqlExcuteReturn(); serr.data = null; serr.error_code = ser.error_code; serr.to = ser.to; return(serr); } } catch (Exception ex) { Logging.Error(ex.Message); throw ex; } }