You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
623 lines
20 KiB
623 lines
20 KiB
using System.Net;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace EC.Util.Net;
|
|
|
|
/// <summary>
|
|
/// 网络操作单元
|
|
/// </summary>
|
|
public partial class NetUnit
|
|
{
|
|
#region 获取主机名称
|
|
|
|
/// <summary>
|
|
/// 获取本地机器名称
|
|
/// </summary>
|
|
/// <returns>机器名称</returns>
|
|
public static string GetHostName()
|
|
{
|
|
string hostName = "";
|
|
try
|
|
{
|
|
hostName = Dns.GetHostName();
|
|
return hostName;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
#endregion 获取主机名称
|
|
|
|
#region 获取本地主机IP
|
|
|
|
/// <summary>
|
|
/// 获取本地机器IP
|
|
/// </summary>
|
|
/// <returns>IP地址</returns>
|
|
public static string GetHostIp()
|
|
{
|
|
string localIp = "";
|
|
try
|
|
{
|
|
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
|
|
for (int i = 0; i < addressList.Length; i++)
|
|
{
|
|
localIp += addressList[i].ToString();
|
|
}
|
|
return localIp;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
#endregion 获取本地主机IP
|
|
|
|
#region 穿过代理服务器获得Ip地址,如果有多个IP,则第一个是用户的真实IP,其余全是代理的IP,用逗号隔开
|
|
|
|
//public static string getRealIp()
|
|
//{
|
|
// string UserIP;
|
|
// if (HttpContext.Current.Request.ServerVariables["HTTP_VIA"]!=null) //得到穿过代理服务器的ip地址
|
|
// {
|
|
// UserIP = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
|
|
// }
|
|
// else
|
|
// {
|
|
// UserIP = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
|
|
// }
|
|
// return UserIP;
|
|
//}
|
|
|
|
#endregion 穿过代理服务器获得Ip地址,如果有多个IP,则第一个是用户的真实IP,其余全是代理的IP,用逗号隔开
|
|
|
|
#region 获取主机MAC地址
|
|
|
|
/// <summary>
|
|
/// 获取本机MAC地址
|
|
/// </summary>
|
|
/// <returns>string null</returns>
|
|
public static string GetHostMac()
|
|
{
|
|
//string mac = "";
|
|
//try
|
|
//{
|
|
// ManagementClass mc;
|
|
// mc=new ManagementClass("Win32_NetworkAdapterConfiguration");
|
|
// ManagementObjectCollection moc=mc.GetInstances();
|
|
// foreach(ManagementObject mo in moc)
|
|
// {
|
|
// if(mo["IPEnabled"].ToString()=="True")
|
|
// mac=mo["MacAddress"].ToString();
|
|
// }
|
|
// return mac;
|
|
//}
|
|
//catch
|
|
//{
|
|
// return null;
|
|
//}
|
|
return null;
|
|
}
|
|
|
|
#endregion 获取主机MAC地址
|
|
|
|
#region 获取本机IP
|
|
|
|
public static string GetLocalIp()
|
|
{
|
|
//Page.Request.UserHostName
|
|
return null;
|
|
}
|
|
|
|
#endregion 获取本机IP
|
|
|
|
/// <summary>
|
|
/// C# 验证IP
|
|
/// </summary>
|
|
/// <param name="ip"></param>
|
|
/// <returns></returns>
|
|
public static bool IsIP(string ip)
|
|
{
|
|
string pattern = @"(((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))\.){3}((\d{1,2})|(1\d{2})|(2[0-4]\d)|(25[0-5]))";
|
|
Regex objRe = new Regex(pattern);
|
|
Match objMa = objRe.Match(ip);
|
|
if (!objMa.Success)
|
|
{
|
|
return false;
|
|
}
|
|
else return true;
|
|
}
|
|
|
|
public static string PingResult(string ip)
|
|
{
|
|
System.Net.NetworkInformation.Ping p = new System.Net.NetworkInformation.Ping();
|
|
System.Net.NetworkInformation.PingOptions options = new System.Net.NetworkInformation.PingOptions();
|
|
options.DontFragment = true;
|
|
string data = "Lxc";
|
|
byte[] buffer = Encoding.ASCII.GetBytes(data);
|
|
//Wait seconds for a reply.
|
|
int timeout = 100;
|
|
|
|
System.Net.NetworkInformation.PingReply reply = p.Send(ip, timeout, buffer, options);
|
|
return reply.Status.ToString();
|
|
}
|
|
|
|
#region 获取指定WEB页面
|
|
|
|
/// GET方法获取页面
|
|
/// 函数名:GetWebUrl
|
|
/// 功能描述:WebClient 获取页面
|
|
/// 处理流程:
|
|
/// 算法描述:
|
|
/// 作 者: lxc
|
|
/// 日 期: 2012-10-09
|
|
/// 修 改:
|
|
/// 日 期:
|
|
/// 版 本:
|
|
public static string GetWebUrl(string strurl)
|
|
{
|
|
WebClient webClient = new WebClient();
|
|
try
|
|
{
|
|
webClient.Credentials = CredentialCache.DefaultCredentials;
|
|
Byte[] pageData = webClient.DownloadData(strurl);
|
|
//string pageHtml = Encoding.UTF8.GetString(pageData);
|
|
string pageHtml = Encoding.Default.GetString(pageData);
|
|
return pageHtml;
|
|
}
|
|
catch (WebException webEx)
|
|
{
|
|
return "error_GetWebUrl:" + webEx.ToString();
|
|
}
|
|
finally
|
|
{
|
|
webClient = null;
|
|
}
|
|
}
|
|
|
|
#endregion 获取指定WEB页面
|
|
|
|
#region PostUrl(String url, String paramList)
|
|
|
|
/// <summary>
|
|
/// POST方法获取页面
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="paramList">格式: a=xxx&b=xxx&c=xxx</param>
|
|
/// <returns></returns>
|
|
public static string PostUrl(String url, String paramList)
|
|
{
|
|
HttpWebResponse res = null;
|
|
string strResult = "";
|
|
try
|
|
{
|
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
req.Method = "POST";
|
|
req.KeepAlive = true;
|
|
req.ContentType = "application/x-www-form-urlencoded";
|
|
//req.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)";
|
|
CookieContainer cookieCon = new CookieContainer();
|
|
req.CookieContainer = cookieCon;
|
|
StringBuilder UrlEncoded = new StringBuilder();
|
|
Char[] reserved = { '?', '=', '&' };
|
|
byte[] SomeBytes = null;
|
|
if (paramList != null)
|
|
{
|
|
int i = 0, j;
|
|
while (i < paramList.Length)
|
|
{
|
|
j = paramList.IndexOfAny(reserved, i);
|
|
if (j == -1)
|
|
{
|
|
// UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length-i)));
|
|
UrlEncoded.Append((paramList.Substring(i, paramList.Length - i)));
|
|
break;
|
|
}
|
|
// UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j-i)));
|
|
UrlEncoded.Append((paramList.Substring(i, j - i)));
|
|
UrlEncoded.Append(paramList.Substring(j, 1));
|
|
i = j + 1;
|
|
}
|
|
SomeBytes = Encoding.Default.GetBytes(UrlEncoded.ToString());
|
|
req.ContentLength = SomeBytes.Length;
|
|
Stream newStream = req.GetRequestStream();
|
|
newStream.Write(SomeBytes, 0, SomeBytes.Length);
|
|
newStream.Close();
|
|
}
|
|
else
|
|
{
|
|
req.ContentLength = 0;
|
|
}
|
|
res = (HttpWebResponse)req.GetResponse();
|
|
Stream ReceiveStream = res.GetResponseStream();
|
|
// Encoding encode = System.Text.Encoding.Default;//GetEncoding("utf-8");
|
|
string encodeheader = res.ContentType;
|
|
string encodestr = System.Text.Encoding.Default.HeaderName;
|
|
if ((encodeheader.IndexOf("charset=") >= 0) && (encodeheader.IndexOf("charset=GBK") == -1) && (encodeheader.IndexOf("charset=gbk") == -1))
|
|
{
|
|
int i = encodeheader.IndexOf("charset=");
|
|
encodestr = encodeheader.Substring(i + 8);
|
|
}
|
|
Encoding encode = System.Text.Encoding.GetEncoding(encodestr);//GetEncoding("utf-8");
|
|
|
|
StreamReader sr = new StreamReader(ReceiveStream, encode);
|
|
Char[] read = new Char[256];
|
|
int count = sr.Read(read, 0, 256);
|
|
while (count > 0)
|
|
{
|
|
String str = new String(read, 0, count);
|
|
strResult += str;
|
|
count = sr.Read(read, 0, 256);
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
strResult = e.ToString();
|
|
}
|
|
finally
|
|
{
|
|
if (res != null)
|
|
{
|
|
res.Close();
|
|
}
|
|
}
|
|
return strResult;
|
|
}
|
|
|
|
#endregion PostUrl(String url, String paramList)
|
|
|
|
#region 下载指定WEB页面
|
|
|
|
/// GET方法获取页面
|
|
/// 函数名:GetFileUrl
|
|
/// 功能描述:WebClient 下载文件
|
|
/// 处理流程:
|
|
/// 算法描述:
|
|
/// 作 者: lxc
|
|
/// 日 期: 2012-10-09
|
|
/// 修 改:
|
|
/// 日 期:
|
|
/// 版 本:
|
|
public static Boolean GetFileUrl(string strurl, string filename)
|
|
{
|
|
try
|
|
{
|
|
WebClient webClient = new WebClient();
|
|
// webClient.Credentials = CredentialCache.DefaultCredentials;
|
|
webClient.DownloadFile(strurl, filename);
|
|
return true;
|
|
}
|
|
catch (WebException webex)
|
|
{
|
|
return false;// "error_GetWebUrl:" + webEx.ToString();
|
|
}
|
|
}
|
|
|
|
#endregion 下载指定WEB页面
|
|
|
|
#region 获取图片
|
|
|
|
public static Stream GetImage(String url)
|
|
{
|
|
HttpWebResponse res = null;
|
|
|
|
try
|
|
{
|
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
res = (HttpWebResponse)req.GetResponse();
|
|
Stream ReceiveStream = res.GetResponseStream();
|
|
return ReceiveStream;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
finally
|
|
{
|
|
}
|
|
}
|
|
|
|
/// GET方法获取页面
|
|
/// 函数名:GetImage
|
|
/// 功能描述:GET方法获取页面
|
|
/// 处理流程:
|
|
/// 算法描述:
|
|
/// 作 者: 杨栋
|
|
/// 日 期: 2006-11-21 09:00
|
|
/// 修 改: 2007-01-29 17:00 2006-12-05 17:00
|
|
/// 日 期:
|
|
/// 版 本:
|
|
///
|
|
|
|
#region GetImage(String url,string cookieheader)
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="url">目标url</param>
|
|
/// <param name="cookieheader">输入Cookie</param>
|
|
/// <returns></returns>
|
|
public static byte[] GetImage(String url, string cookieheader)
|
|
{
|
|
// outcookieheader="";
|
|
|
|
HttpWebResponse res = null;
|
|
|
|
try
|
|
{
|
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
// req.Method = "GET";
|
|
// req.ContentType = "application/x-www-form-urlencoded";
|
|
//req.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)";
|
|
// CookieContainer cookieCon = new CookieContainer();
|
|
//
|
|
// // string name=cookieheader.Substring(0,cookieheader.IndexOf("="));
|
|
// // string key=cookieheader.Substring(cookieheader.IndexOf("=")+1,cookieheader.Length-cookieheader.IndexOf("=")-1);
|
|
// // cookieCon.Add(new Uri(url),new Cookie(name,key));
|
|
//
|
|
//
|
|
// req.CookieContainer = cookieCon;
|
|
// if ((cookieheader.Length>0 )&(cookieheader.IndexOf("=")>0))
|
|
// {
|
|
// req.CookieContainer.SetCookies(new Uri(url),cookieheader);
|
|
// }
|
|
|
|
//为请求加入cookies
|
|
CookieContainer cookieCon = new CookieContainer();
|
|
// req.CookieContainer = cookieCon;
|
|
//取得cookies 集合
|
|
string[] ls_cookies = cookieheader.Split(';');
|
|
if (ls_cookies.Length <= 1) //如果有一个或没有cookies 就采用下面的方法。
|
|
{
|
|
req.CookieContainer = cookieCon;
|
|
if ((cookieheader.Length > 0) & (cookieheader.IndexOf("=") > 0))
|
|
{
|
|
req.CookieContainer.SetCookies(new Uri(url), cookieheader);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//如果是多个cookie 就分别加入 cookies 容器。
|
|
//////////////////////////////////
|
|
string[] ls_cookie = null;
|
|
|
|
for (int i = 0; i < ls_cookies.Length; i++)
|
|
{
|
|
ls_cookie = ls_cookies[i].Split('=');
|
|
// cookieCon.Add(new Uri(url),new Cookie(ls_cookie[0].ToString().Trim(),ls_cookie[1].ToString().Trim()));
|
|
cookieCon.Add(new Uri(url), new Cookie(ls_cookie[0].ToString().Trim(), ls_cookies[i].Substring(ls_cookies[i].IndexOf("=") + 1)));
|
|
}
|
|
req.CookieContainer = cookieCon;
|
|
|
|
////////////////////////////////////
|
|
}
|
|
|
|
res = (HttpWebResponse)req.GetResponse();
|
|
Stream ReceiveStream = res.GetResponseStream();
|
|
|
|
byte[] mybytes = new byte[4096];
|
|
|
|
int count = ReceiveStream.Read(mybytes, 0, 4096);
|
|
|
|
byte[] image = new byte[count];
|
|
|
|
Array.Copy(mybytes, image, count);
|
|
|
|
if (res != null)
|
|
{
|
|
res.Close();
|
|
}
|
|
return image;
|
|
}
|
|
finally
|
|
{
|
|
}
|
|
}
|
|
|
|
#endregion GetImage(String url,string cookieheader)
|
|
|
|
#region GetImage(String url,string cookieheader,out string outcookieheader,string Header_Referer)
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="url">目标url</param>
|
|
/// <param name="cookieheader">输入Cookie</param>
|
|
/// <returns></returns>
|
|
public static byte[] GetImage(String url, string cookieheader, out string outcookieheader, string Header_Referer)
|
|
{
|
|
// outcookieheader="";
|
|
|
|
HttpWebResponse res = null;
|
|
|
|
try
|
|
{
|
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
if (Header_Referer.Length > 1)
|
|
{
|
|
req.Referer = Header_Referer;
|
|
}
|
|
// req.Method = "GET";
|
|
// req.ContentType = "application/x-www-form-urlencoded";
|
|
//req.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)";
|
|
// CookieContainer cookieCon = new CookieContainer();
|
|
//
|
|
// // string name=cookieheader.Substring(0,cookieheader.IndexOf("="));
|
|
// // string key=cookieheader.Substring(cookieheader.IndexOf("=")+1,cookieheader.Length-cookieheader.IndexOf("=")-1);
|
|
// // cookieCon.Add(new Uri(url),new Cookie(name,key));
|
|
//
|
|
//
|
|
// req.CookieContainer = cookieCon;
|
|
// if ((cookieheader.Length>0 )&(cookieheader.IndexOf("=")>0))
|
|
// {
|
|
// req.CookieContainer.SetCookies(new Uri(url),cookieheader);
|
|
// }
|
|
|
|
//为请求加入cookies
|
|
CookieContainer cookieCon = new CookieContainer();
|
|
// req.CookieContainer = cookieCon;
|
|
//取得cookies 集合
|
|
string[] ls_cookies = cookieheader.Split(';');
|
|
if (ls_cookies.Length <= 1) //如果有一个或没有cookies 就采用下面的方法。
|
|
{
|
|
req.CookieContainer = cookieCon;
|
|
if ((cookieheader.Length > 0) & (cookieheader.IndexOf("=") > 0))
|
|
{
|
|
req.CookieContainer.SetCookies(new Uri(url), cookieheader);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//如果是多个cookie 就分别加入 cookies 容器。
|
|
//////////////////////////////////
|
|
string[] ls_cookie = null;
|
|
|
|
for (int i = 0; i < ls_cookies.Length; i++)
|
|
{
|
|
ls_cookie = ls_cookies[i].Split('=');
|
|
// cookieCon.Add(new Uri(url),new Cookie(ls_cookie[0].ToString().Trim(),ls_cookie[1].ToString().Trim()));
|
|
cookieCon.Add(new Uri(url), new Cookie(ls_cookie[0].ToString().Trim(), ls_cookies[i].Substring(ls_cookies[i].IndexOf("=") + 1)));
|
|
}
|
|
req.CookieContainer = cookieCon;
|
|
|
|
////////////////////////////////////
|
|
}
|
|
|
|
res = (HttpWebResponse)req.GetResponse();
|
|
Stream ReceiveStream = res.GetResponseStream();
|
|
|
|
outcookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));//获得cookie
|
|
|
|
if (outcookieheader.Length < 2)
|
|
{
|
|
try
|
|
{
|
|
outcookieheader = res.Headers["Set-Cookie"];
|
|
outcookieheader = outcookieheader.Substring(0, outcookieheader.IndexOf(";"));
|
|
}
|
|
catch
|
|
{
|
|
outcookieheader = "";
|
|
}
|
|
}
|
|
|
|
byte[] mybytes = new byte[4096];
|
|
|
|
int count = ReceiveStream.Read(mybytes, 0, 4096);
|
|
|
|
byte[] image = new byte[count];
|
|
|
|
Array.Copy(mybytes, image, count);
|
|
|
|
if (res != null)
|
|
{
|
|
res.Close();
|
|
}
|
|
return image;
|
|
}
|
|
finally
|
|
{
|
|
}
|
|
}
|
|
|
|
#endregion GetImage(String url,string cookieheader,out string outcookieheader,string Header_Referer)
|
|
|
|
#region GetImage(String url,string cookieheader,string Header_Referer)
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="url">目标url</param>
|
|
/// <param name="cookieheader">输入Cookie</param>
|
|
/// <param name="Header_Referer">包头 Referer</param>
|
|
/// <returns></returns>
|
|
public static byte[] GetImage(String url, string cookieheader, string Header_Referer)
|
|
{
|
|
// outcookieheader="";
|
|
|
|
HttpWebResponse res = null;
|
|
|
|
try
|
|
{
|
|
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
|
|
if (Header_Referer.Length > 1)
|
|
{
|
|
req.Referer = Header_Referer;
|
|
}
|
|
// req.Method = "GET";
|
|
// req.ContentType = "application/x-www-form-urlencoded";
|
|
//req.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)";
|
|
// CookieContainer cookieCon = new CookieContainer();
|
|
//
|
|
// // string name=cookieheader.Substring(0,cookieheader.IndexOf("="));
|
|
// // string key=cookieheader.Substring(cookieheader.IndexOf("=")+1,cookieheader.Length-cookieheader.IndexOf("=")-1);
|
|
// // cookieCon.Add(new Uri(url),new Cookie(name,key));
|
|
//
|
|
//
|
|
// req.CookieContainer = cookieCon;
|
|
// if ((cookieheader.Length>0 )&(cookieheader.IndexOf("=")>0))
|
|
// {
|
|
// req.CookieContainer.SetCookies(new Uri(url),cookieheader);
|
|
// }
|
|
|
|
//为请求加入cookies
|
|
CookieContainer cookieCon = new CookieContainer();
|
|
// req.CookieContainer = cookieCon;
|
|
//取得cookies 集合
|
|
string[] ls_cookies = cookieheader.Split(';');
|
|
if (ls_cookies.Length <= 1) //如果有一个或没有cookies 就采用下面的方法。
|
|
{
|
|
req.CookieContainer = cookieCon;
|
|
if ((cookieheader.Length > 0) & (cookieheader.IndexOf("=") > 0))
|
|
{
|
|
req.CookieContainer.SetCookies(new Uri(url), cookieheader);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//如果是多个cookie 就分别加入 cookies 容器。
|
|
//////////////////////////////////
|
|
string[] ls_cookie = null;
|
|
|
|
for (int i = 0; i < ls_cookies.Length; i++)
|
|
{
|
|
ls_cookie = ls_cookies[i].Split('=');
|
|
// cookieCon.Add(new Uri(url),new Cookie(ls_cookie[0].ToString().Trim(),ls_cookie[1].ToString().Trim()));
|
|
cookieCon.Add(new Uri(url), new Cookie(ls_cookie[0].ToString().Trim(), ls_cookies[i].Substring(ls_cookies[i].IndexOf("=") + 1)));
|
|
}
|
|
req.CookieContainer = cookieCon;
|
|
|
|
////////////////////////////////////
|
|
}
|
|
|
|
res = (HttpWebResponse)req.GetResponse();
|
|
Stream ReceiveStream = res.GetResponseStream();
|
|
|
|
byte[] mybytes = new byte[4096];
|
|
|
|
int count = ReceiveStream.Read(mybytes, 0, 4096);
|
|
|
|
byte[] image = new byte[count];
|
|
|
|
Array.Copy(mybytes, image, count);
|
|
|
|
if (res != null)
|
|
{
|
|
res.Close();
|
|
}
|
|
return image;
|
|
}
|
|
finally
|
|
{
|
|
}
|
|
}
|
|
|
|
#endregion GetImage(String url,string cookieheader,string Header_Referer)
|
|
|
|
#endregion 获取图片
|
|
}
|