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
{
///
/// 把Model转换为DataRow
///
///
///
///
public static T ParseDictionaryToModel(Dictionary dict)
{
//
T obj = default(T);
obj = Activator.CreateInstance();
//根据Key值设定 Columns
foreach (KeyValuePair 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;
}
}
}