using System;
|
using System.Web;
|
using System.Text;
|
using System.Net;
|
using System.IO;
|
|
namespace WebApp
|
{
|
public class Utility
|
{
|
/// <summary>
|
/// 读取BasicAuth Ticket
|
/// </summary>
|
/// <returns></returns>
|
public static string GetBasicAuthTicket()
|
{
|
cookidLoginUser m = new cookidLoginUser();
|
m = GetLoginUser();
|
if (m != null)
|
{
|
string cookieGuid = m.loginGuid;//登录者guid
|
return "BasicAuth " + GetTimestamp().ToString() + "_" + cookieGuid;
|
}
|
else
|
return "BasicAuth " + GetTimestamp().ToString() + "_" + "null";
|
|
}
|
/// <summary>
|
/// 读取登录实体
|
/// </summary>
|
/// <returns></returns>
|
public static cookidLoginUser GetLoginUser()
|
{
|
HttpCookie cookies = HttpContext.Current.Request.Cookies["bthUserInfo"];//取cookie
|
if (cookies == null)
|
return null;
|
cookidLoginUser m = new cookidLoginUser();
|
m.loginGuid = HttpUtility.UrlDecode(cookies.Values["loginGuid".ToUpper()]);//这是用户的guid
|
m.loginAgtGuid = HttpUtility.UrlDecode(cookies.Values["loginAgtGuid".ToUpper()]);
|
m.loginRight = HttpUtility.UrlDecode(cookies.Values["loginRight".ToUpper()]);
|
m.loginRightList = HttpUtility.UrlDecode(cookies.Values["loginRightList".ToUpper()]);
|
return m;
|
}
|
|
/// <summary>
|
/// http get 读取数据
|
/// </summary>
|
/// <param name="url"></param>
|
/// <returns></returns>
|
public static string HttpGet(string url)
|
{
|
Encoding encoding = Encoding.UTF8;
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
request.Method = "GET";
|
request.Accept = "text/html, application/xhtml+xml, */*";
|
request.ContentType = "application/json";
|
request.Headers.Add("Authorization", GetBasicAuthTicket());
|
try
|
{
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
|
{
|
return reader.ReadToEnd();
|
}
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
}
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="url"></param>
|
/// <param name="param"> @"{ ""mmmm"": ""89e"",""nnnnnn"": ""0101943"",""kkkkkkk"": ""e8sodijf9""}"</param>
|
/// <returns></returns>
|
public static string HttpPost(string url, string param)
|
{
|
string serviceAddress = url;
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress);
|
request.Method = "POST";
|
request.ContentType = "application/json";
|
request.Headers.Add("Authorization", GetBasicAuthTicket());
|
string strContent = param;
|
using (StreamWriter dataStream = new StreamWriter(request.GetRequestStream()))
|
{
|
dataStream.Write(strContent);
|
dataStream.Close();
|
}
|
try
|
{
|
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
|
string encoding = response.ContentEncoding;
|
if (encoding == null || encoding.Length < 1)
|
{
|
encoding = "UTF-8"; //默认编码
|
}
|
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
|
string retString = reader.ReadToEnd();
|
return retString;
|
}
|
catch (System.Net.WebException ex)
|
{
|
throw ex;
|
}
|
|
|
}
|
|
|
/// <summary>
|
/// 读取时间戳
|
/// </summary>
|
/// <param name="d"></param>
|
/// <returns></returns>
|
public static double GetTimestamp()
|
{
|
DateTime d = DateTime.Now;
|
TimeSpan ts = d.ToUniversalTime() - new DateTime(1970, 1, 1);
|
return ts.TotalMilliseconds; //精确到毫秒
|
}
|
|
/**
|
* 向日志文件写入出错信息
|
* @param className 类名
|
* @param content 写入内容
|
*/
|
public static void Error(string className, string content)
|
{
|
WriteLog("ERROR", className, content);
|
}
|
|
/**
|
* 实际的写日志操作
|
* @param type 日志记录类型
|
* @param className 类名
|
* @param content 写入内容
|
*/
|
private static void WriteLog(string type, string className, string content)
|
{
|
string path = HttpContext.Current.Server.MapPath("~") + "logs";
|
if (!Directory.Exists(path))
|
Directory.CreateDirectory(path);
|
|
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");//获取当前系统时间
|
string filename = path + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".log";//用日期对日志文件命名
|
|
//创建或打开日志文件,向日志文件末尾追加记录
|
StreamWriter mySw = File.AppendText(filename);
|
|
//向日志文件写入内容
|
string write_content = time + " " + type + " " + className + ": " + content;
|
mySw.WriteLine(write_content);
|
|
//关闭日志文件
|
mySw.Close();
|
}
|
}
|
|
public class cookidLoginUser
|
{
|
|
/// <summary>
|
/// 登录者guid
|
/// </summary>
|
public string loginGuid
|
{
|
set;
|
get;
|
}
|
|
/// <summary>
|
/// 登录者agtGuid
|
/// </summary>
|
public string loginAgtGuid
|
{
|
set;
|
get;
|
}
|
/// <summary>
|
/// 登录者rightloginRightList
|
/// </summary>
|
public string loginRight
|
{
|
set;
|
get;
|
}
|
/// <summary>
|
/// 登录者loginRightList
|
/// </summary>
|
public string loginRightList
|
{
|
set;
|
get;
|
}
|
}
|
}
|