|
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using ICSSoft.Frame.Data.Entity; using ICSSoft.Base.Config.AppConfig; using ICSSoft.Base.Config.DBHelper; using System.Data; using System.Net; using System.IO; using Newtonsoft.Json;
namespace ICSSoft.Frame.Data.DAL { public class ICSKodDbApiDAL { public static DataTable GetFileDirectory(string conn, string WorkPoint, string id, string name, string parentName) { //获取api地址
string sql = "SELECT DBIpAddress,DBName FROM dbo.Sys_DataBase WHERE DBSourceName='KodDbApi' AND WorkCode='" + WorkPoint + "'"; DataTable dtApiAddress = DBHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0]; if (dtApiAddress.Rows.Count == 0) { throw new Exception("获取可道云自建api地址失败,KodDbApi未维护!"); } string host = dtApiAddress.Rows[0][0].ToString().Replace("http:", "").Trim(new char[] { ',', '/', '\\' }); string siteName = dtApiAddress.Rows[0][1].ToString().Trim(new char[] { ',', '/', '\\' }); string url = "http://" + host + "/" + siteName + "/api/FileDirectory"; //StringBuilder inputData = new StringBuilder();
//inputData.Append("{");
//inputData.AppendFormat("\"baseSourceid\":\"{0}\",", id);
//inputData.AppendFormat("\"name\":\"{0}\",", name);
//inputData.AppendFormat("\"parentName\":\"{0}\"", parentName);
//inputData.Append("}");
Input4FileDirectory inputData = new Input4FileDirectory(); inputData.baseSourceid = id; inputData.name = name; inputData.parentName = parentName; string @in = JsonConvert.SerializeObject(inputData); string resultData = HttpPost(url, @in);
Result result = JsonConvert.DeserializeObject<Result>(resultData); if (result.data == null) { throw new Exception("未找到此物料对应的资料" + name); } DataTable dt = JsonConvert.DeserializeObject<DataTable>(result.data); return dt; }
/// <summary>
/// 接口调用方法
/// </summary>
/// <param name="url"></param>
/// <param name="body"></param>
/// <returns></returns>
private static string HttpPost(string url, string body) { try { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
request.ContentType = "application/json; charset=utf-8";
byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (WebException ex) { if (ex.Response != null) { var res = (HttpWebResponse)ex.Response; StringBuilder sb = new StringBuilder(); using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8)) { sb.Append(sr.ReadToEnd()); } throw new Exception(sb.ToString()); } throw new Exception(ex.Message); } } } }
|