123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.IO;
- using Newtonsoft.Json.Linq;
- using Newtonsoft.Json;
- using SupplierWeb.Service;
- using System.Reflection;
- namespace SupplierWeb.Commonss
- {
- public static class Util
- {
- /// <summary>
- /// 把Model转换为DataRow
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="mod"></param>
- /// <returns></returns>
- public static T ParseDictionaryToModel<T>(Dictionary<string, object> dict)
- {
- //
- T obj = default(T);
- obj = Activator.CreateInstance<T>();
-
- //根据Key值设定 Columns
- foreach (KeyValuePair<string, object> item in dict)
- {
- PropertyInfo prop = obj.GetType().GetProperty(item.Key);
- if(null != prop && null != (item.Value))
- {
- object value = item.Value;
- //Nullable 获取Model类字段的真实类型
- Type itemType = Nullable.GetUnderlyingType(prop.PropertyType) == null ? prop.PropertyType : Nullable.GetUnderlyingType(prop.PropertyType);
- //根据Model类字段的真实类型进行转换
- prop.SetValue(obj, Convert.ChangeType(value, itemType), null);
- }
- }
- return obj;
- }
- static string HOST = "http://192.168.50.32:9200/";
- public static string ElasticCall(string url, string method, string query)
- {
- HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(HOST + url);
- myReq.Method = method;
- var headers = new WebHeaderCollection();
- headers.Add("Content-Type", "application/json");
- myReq.Headers = headers;
- if (method == "POST")
- {
- using (var streamWriter = new StreamWriter(myReq.GetRequestStream()))
- {
- string json = query;
- streamWriter.Write(json);
- streamWriter.Flush();
- streamWriter.Close();
- }
- }
- HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
- System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
- string responseString = respStreamReader.ReadToEnd();
- respStreamReader.Close();
- myResp.Close();
- return responseString;
- }
- public static string ElasticQuery(string url, string method, string query)
- {
- HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(HOST + url);
- myReq.Method = method;
- var headers = new WebHeaderCollection();
- headers.Add("Content-Type", "application/json");
- myReq.Headers = headers;
- if (method == "POST")
- {
- using (var streamWriter = new StreamWriter(myReq.GetRequestStream()))
- {
- string json = query;
- streamWriter.Write(json);
- streamWriter.Flush();
- streamWriter.Close();
- }
- }
- HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
- System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
- string responseString = respStreamReader.ReadToEnd();
- respStreamReader.Close();
- myResp.Close();
- return responseString;
- }
- public static JObject SqlQuery(string query)
- {
- var json = "{\"query\":\"" + query +
- "\"}";
- var str = ElasticCall( "_sql?format=json", "POST", json);
- var result = (JObject)JsonConvert.DeserializeObject(str);
- return result;
- }
- public static string WechatHttp(string url, string method, string query)
- {
- HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
- myReq.Method = method;
- var headers = new WebHeaderCollection();
- headers.Add("Content-Type", "application/json");
- myReq.Headers = headers;
- if (method == "POST")
- {
- using (var streamWriter = new StreamWriter(myReq.GetRequestStream()))
- {
- string json = query;
- streamWriter.Write(json);
- streamWriter.Flush();
- streamWriter.Close();
- }
- }
- HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
- System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
- string responseString = respStreamReader.ReadToEnd();
- respStreamReader.Close();
- myResp.Close();
- return responseString;
- }
- }
- }
|