/// <summary> /// 生成二维码图片 /// </summary> /// <param name="codeContent"></param> /// <param name="fileName"></param> /// <returns></returns> public static bool CreateImgCode(string codeContent, string fileName) { bool flag = false; try { string path = Path.GetDirectoryName(fileName); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //创建二维码生成类 QrEncoder qrEncoder = new QrEncoder(ErrorCorrectionLevel.M); QrCode qrCode = new QrCode(); qrEncoder.TryEncode(codeContent, out qrCode); GraphicsRenderer gRender = new GraphicsRenderer(new FixedModuleSize(30, QuietZoneModules.Four)); BitMatrix matrix = qrCode.Matrix; //qrCodeGraphicControl1.GetQrMatrix(); using (FileStream stream = new FileStream(fileName, FileMode.Create)) { gRender.WriteToStream(matrix, System.Drawing.Imaging.ImageFormat.Png, stream, new Point(600, 600)); } flag = true; } catch (Exception ex) { ClassLoger.Error("QrCodeHelper.Create_ImgCode"); } return(flag); }
/// <summary> /// GET请求获取信息 /// </summary> /// <param name="url"></param> /// <returns></returns> public string GET(string url, string encode = "UTF-8", int timeout = 5000, List <Cookie> cookies = null) { ClassLoger.Info("HttpUtils.Get", url); string ret = string.Empty; try { HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(url); if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc; web.ProtocolVersion = HttpVersion.Version10; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; } SetProxy(ref web); web.Method = "GET"; web.Timeout = timeout; web.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; //web.Connection = "keep-alive"; web.KeepAlive = true; web.IfModifiedSince = DateTime.Now; web.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8"); web.Headers.Add("X-CSRF-Token", "aDkFA0RIlVpHySSCFTkhQ62fPUzqhpFzC8nWxIUlgYOZwYtGdkFQwL6Ppii1/ngiCT9sSkSGIS2EMO4vrXUmqA=="); web.Date = DateTime.Now; if (!string.IsNullOrEmpty(UserAgent)) { web.UserAgent = UserAgent; } if (cookies != null && cookies.Count > 0) { web.CookieContainer = new CookieContainer(); string host = new Uri(url).Host; foreach (Cookie c in cookies) { c.Domain = host; web.CookieContainer.Add(c); } } HttpWebResponse res = (HttpWebResponse)web.GetResponse(); Stream s = res.GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.GetEncoding(encode)); ret = sr.ReadToEnd(); sr.Close(); res.Close(); s.Close(); //ClassLoger.Error("HttpUtils/GET",url+" "+ ret); } catch (Exception ex) { ClassLoger.Error("HttpUtils/GET", url + "," + ex.Message); throw ex; } return(ret); }
/// <summary> /// POST请求获取信息(上传) /// </summary> /// <param name="url"></param> /// <param name="paramData"></param> /// <returns></returns> public string POST(string url, byte[] data, int timeout = 5000, List <Cookie> cookies = null) { string ret = string.Empty; try { string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url)); if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc; webReq.ProtocolVersion = HttpVersion.Version10; } SetProxy(ref webReq); webReq.Method = "POST"; webReq.ContentType = "multipart/form-data; boundary=" + boundary; webReq.ServicePoint.Expect100Continue = false; //webReq.ContentType = "text/html;charset=utf-8"; webReq.Timeout = timeout; webReq.ContentLength = data.Length; if (!string.IsNullOrEmpty(Token)) { webReq.Headers.Add("Authorization", Token); } if (!string.IsNullOrEmpty(UserAgent)) { webReq.UserAgent = UserAgent; } if (cookies != null && cookies.Count > 0) { webReq.CookieContainer = new CookieContainer(); string host = new Uri(url).Host; foreach (Cookie c in cookies) { c.Domain = host; webReq.CookieContainer.Add(c); } } Stream newStream = webReq.GetRequestStream(); newStream.Write(data, 0, data.Length);//写入参数 newStream.Close(); HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8); ret = sr.ReadToEnd(); sr.Close(); response.Close(); newStream.Close(); } catch (Exception ex) { ClassLoger.Error("HttpUtils/POST", url + "," + ex.Message); throw ex; } return(ret); }
public string GETbyJson(string url, string encode = "UTF-8", int timeout = 5000, List <Cookie> cookies = null) { string ret = string.Empty; try { HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(url); if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc; web.ProtocolVersion = HttpVersion.Version10; } SetProxy(ref web); web.Method = "GET"; web.Timeout = timeout; //web.Headers.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"); web.Headers.Add("Accept-Language", "zh-CN,zh;q=0.8"); //web.Headers["Connection"] = "keep-alive"; //web.Headers.Add("Connection", "keep-alive"); web.ContentType = "application/json"; if (!string.IsNullOrEmpty(UserAgent)) { web.UserAgent = UserAgent; } if (!string.IsNullOrEmpty(Token)) { web.Headers.Add("Authorization", Token); } if (cookies != null && cookies.Count > 0) { web.CookieContainer = new CookieContainer(); string host = new Uri(url).Host; foreach (Cookie c in cookies) { c.Domain = host; web.CookieContainer.Add(c); } } HttpWebResponse res = (HttpWebResponse)web.GetResponse(); Stream s = res.GetResponseStream(); StreamReader sr = new StreamReader(s, Encoding.GetEncoding(encode)); ret = sr.ReadToEnd(); sr.Close(); res.Close(); s.Close(); //ClassLoger.Error("HttpUtils/GET",url+" "+ ret); } catch (Exception ex) { ClassLoger.Error("HttpUtils/GET", url + "," + ex.Message); throw ex; } return(ret); }
/// <summary> /// 将类序列化成 字符串 /// </summary> /// <param name="obj"></param> /// <returns></returns> public static string SerializeObject(object obj) { try { JavaScriptSerializer jss = new JavaScriptSerializer(); return(jss.Serialize(obj)); } catch (Exception ex) { ClassLoger.Error("JsonHelper/DeserializeObject", ex.Message); } return(null); }
/// <summary> /// 解析json 返回对应的对象 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="jsonstr"></param> /// <returns></returns> public static T DeserializeObject <T>(string jsonstr) { try { if (!string.IsNullOrEmpty(jsonstr)) { JavaScriptSerializer jss = new JavaScriptSerializer(); return(jss.Deserialize <T>(jsonstr)); } } catch (Exception ex) { ClassLoger.Error("JsonHelper/DeserializeObject", ex.Message); } return(default(T)); }
/// <summary> /// 解析json 返回object 数组 /// </summary> /// <param name="jsonstr"></param> /// <returns></returns> public static object[] Deserialize(string jsonstr) { try { if (!string.IsNullOrEmpty(jsonstr)) { JavaScriptSerializer jss = new JavaScriptSerializer(); object d = jss.DeserializeObject(jsonstr); return((object[])d); } } catch (Exception ex) { ClassLoger.Error("JsonHelper/Deserialize", ex.Message); } return(null); }
/// <summary> /// 解析json 返回dict对象 /// </summary> /// <param name="jsonstr"></param> /// <returns></returns> public static Dictionary <string, object> DeserializeObject(string jsonstr) { List <object> list = new List <object>(); try { if (!string.IsNullOrEmpty(jsonstr)) { JavaScriptSerializer jss = new JavaScriptSerializer(); object d = jss.DeserializeObject(jsonstr); return((Dictionary <string, object>)d); } } catch (Exception ex) { ClassLoger.Error("JsonHelper/DeserializeObject", ex.Message); } return(null); }
/// <summary> /// 判断文件是否有更新 /// </summary> /// <param name="url"></param> /// <param name="localmodifieddate"></param> /// <returns></returns> public bool FileIsModified(string url, DateTime localmodifieddate, int timeout = 5000) { bool isModified = true; try { HttpWebRequest webReq = (HttpWebRequest)WebRequest.Create(new Uri(url)); if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc; webReq.ProtocolVersion = HttpVersion.Version10; } SetProxy(ref webReq); webReq.Method = "GET"; webReq.IfModifiedSince = localmodifieddate; webReq.Timeout = timeout; HttpWebResponse response = (HttpWebResponse)webReq.GetResponse(); response.Close(); } catch (WebException wex) { HttpWebResponse response = (HttpWebResponse)wex.Response; if (response.StatusCode == HttpStatusCode.NotModified) { isModified = false; } response.Close(); } catch (Exception ex) { ClassLoger.Error("HttpUtils/FileIsModified", url, ex); throw ex; } return(isModified); }
/// <summary> /// 下载文件 /// </summary> /// <param name="url">下载地址</param> /// <param name="savepath">保存路径</param> public bool DownFile(string url, string savepath) { if (url.IsNull() || savepath.IsNull()) { return(false); } try { string dir_path = savepath.Replace(Path.GetFileName(savepath), ""); if (!Directory.Exists(dir_path)) { Directory.CreateDirectory(dir_path); } HttpWebRequest web = (HttpWebRequest)HttpWebRequest.Create(url); if (url.StartsWith("https://", StringComparison.OrdinalIgnoreCase)) { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate); //rcvc; web.ProtocolVersion = HttpVersion.Version10; } SetProxy(ref web); web.Method = "GET"; if (!string.IsNullOrEmpty(UserAgent)) { web.UserAgent = UserAgent; } HttpWebResponse res = (HttpWebResponse)web.GetResponse(); Stream s = res.GetResponseStream(); byte[] data = new byte[1024 * 100]; using (FileStream fs = File.Open(savepath, FileMode.Create)) { int count = 0; while ((count = s.Read(data, 0, data.Length)) > 0) { fs.Write(data, 0, count); } fs.Close(); fs.Dispose(); } s.Close(); s.Dispose(); res.Close(); if (File.Exists(savepath)) { return(true); } else { return(false); } //ClassLoger.Error("HttpUtils/GET",url+" "+ ret); } catch (Exception ex) { if (File.Exists(savepath)) { File.Delete(savepath); } ClassLoger.Error("HttpUtils/GET", url + "," + ex.Message); throw ex; } }