厚积薄发,丰富的公用类库积累,助你高效进行系统开发(12)--- 网络相关操作辅助类

753 查看

俗话说,一个好汉十个帮,众人拾柴火焰高等都说明一个道理,有更多的资源,更丰富的积累,都是助你走向成功,走向顶峰的推动力。

本篇的公用类库的介绍主题是程序开发中多线程操作环境中,常用到的网络操作相关类,本篇随笔介绍包含邮件发送辅助类(包含附件、嵌入图片等)、获取网页数据辅助类库、管理文档服务器类、网络相关操作辅助类、IE代理设置辅助类等对象,这些辅助类覆盖了网络编程开发中多数的应用。良好的封装及操作,给我们提供非常方便、高效的辅助类库操作体验。

本篇继续继续整理优化已有的共用类库,并继续发表随笔介绍公用类库的接口方法以及详细使用操作,力求给自己继续优化,积攒更丰富的公用类库资源,加深了解的同时,也给大家展现公用类库好的方面。
1、发送邮件的辅助类 EmailHelper。

实现效果

1) 本辅助类主要是用来方便实现发送邮件操作,可以发送附件、嵌入图片、HTML等内容邮件。使用底层SMTP协议指令进行发送,成功率较高。

2)邮件发送可以用在客户反馈,信息推广,客户沟通,员工交流等方面业务需要。

实现代码

1)辅助类提供的方法源码如下所示:

/// <summary>    
/// 设定语言代码,默认设定为GB2312,如不需要可设置为""    
/// </summary>
public string Charset = "GB2312";    

/// <summary>    
/// 邮箱服务器    
/// </summary>
public string MailServer    

/// <summary>    
/// 邮件服务器端口号,默认端口为25    
/// </summary>
public int MailServerPort    

/// <summary>    
/// SMTP认证时使用的用户名    
/// </summary>
public string MailServerUsername    

/// <summary>    
/// SMTP认证时使用的密码    
/// </summary>
public string MailServerPassword    

/// <summary>    
/// 发件人地址    
/// </summary>
public string From    

/// <summary>    
/// 发件人姓名    
/// </summary>
public string FromName    

/// <summary>    
/// 回复邮件地址    
/// </summary>
public string ReplyTo = "";    

/// <summary>    
/// 邮件主题    
/// </summary>
public string Subject = "";    

/// <summary>    
/// 是否Html邮件    
/// </summary>
public bool IsHtml = false;    

/// <summary>    
/// 收件人是否发送收条    
/// </summary>
public bool ReturnReceipt = false;    

/// <summary>    
/// 邮件正文    
/// </summary>
public string Body = "";    

/// <summary>    
/// 邮件发送优先级,可设置为"High","Normal","Low"或"1","3","5"    
/// </summary>
public string Priority    

/// <summary>    
/// 错误消息反馈    
/// </summary>
public string ErrorMessage    

/// <summary>    
/// 收件人姓名    
/// </summary>
public string RecipientName = "";    

/// <summary>    
/// 默认构造函数    
/// </summary>
public EmailHelper()    

/// <summary>    
/// 待邮箱发送配置参数的构造函数    
/// </summary>    
/// <param name="mailServer">邮件服务器</param>    
/// <param name="username">用户名</param>    
/// <param name="password">用户密码</param>
public EmailHelper(string mailServer, string username, string password)     

/// <summary>    
/// 待邮箱发送配置参数的构造函数    
/// </summary>    
/// <param name="mailServer">邮件服务器</param>    
/// <param name="username">用户名</param>    
/// <param name="password">用户密码</param>    
/// <param name="mailServerPort">邮箱服务器端口</param>
public EmailHelper(string mailServer, string username, string password, int port)     

 /// <summary>    
/// 添加一个附件,需使用绝对路径    
/// </summary>
public bool AddAttachment(string path)    

/// <summary>    
/// 添加一个收件人    
/// </summary>      
/// <param name="str">收件人地址</param>
public bool AddRecipient(string str)           

 /// <summary>    
/// 添加一个抄送收件人    
/// </summary>    
/// <param name="str">收件人地址</param>
public bool AddRecipientCC(string str)    

/// <summary>    
/// 添加一个密件收件人    
/// </summary>    
/// <param name="str">收件人地址</param>
public bool AddRecipientBCC(string str)    

/// <summary>    
/// 清空收件人列表    
/// </summary>
public void ClearRecipient()    

/// <summary>    
/// 发送邮件    
/// </summary>
public bool SendEmail()

2)辅助类EmailHelper的使用例子代码如下所示

EmailHelper email = new EmailHelper("smtp.163.com", "wuhuacong2013@163.com", "password");    
email.Subject = "伍华聪的普通测试邮件";    
email.Body = string.Format("测试邮件正文内容");    
email.IsHtml = true;    
email.From = "wuhuacong2013@163.com";    
email.FromName = "wuhuacong2013";    
email.AddRecipient("6966254@qq.com");    
try   
{    
    bool success = email.SendEmail();    
    MessageUtil.ShowTips(success ? "发送成功" : "发送失败");    
}    
catch (Exception ex)    
{    
    MessageUtil.ShowError(ex.Message);    
}


3)如果使用发送附件、发送嵌入图片(正文中有图片显示内容的)方式,则例子代码如下所示。

EmailHelper email = new EmailHelper("smtp.163.com", "wuhuacong2013@163.com", "password");    
email.Subject = "伍华聪的图片附件测试邮件";    
string embedFile = Path.Combine(Application.StartupPath, "cityroad.jpg");    
email.Body = string.Format("测试邮件正文内容<img src=\"{0}\" title='测试图片' /> ", embedFile);    
email.IsHtml = true;    
email.From = "wuhuacong2013@163.com";    
email.FromName = "wuhuacong2013";    
email.AddRecipient("6966254@qq.com");    
email.AddAttachment(Path.Combine(Application.StartupPath, "ringin.wav"));//.AddAttachment("C:\\test.txt");    

try   
{    
    bool success = email.SendEmail();    
    MessageUtil.ShowTips(success ? "发送成功" : "发送失败");     
}    
catch (Exception ex)    
{    
    MessageUtil.ShowError(ex.Message);    
}

2、获取网页数据辅助类库 HttpHelper。
实现效果
1) 本辅助类主要是用来方便实现获取网页数据的操作,可以通过GET、POST方式获取网页内容,获取验证码等图片资源,是网络编程不可或缺的强大辅助类库。
2) 该辅助类库在我的QQ搜通天系列软件、QQ群成员提取工具、易博搜搜等网络应用软件上,辅助类主要是用于网页数据采集和分析操作。

实现代码
1)辅助类提供的方法源码如下所示:

#region 属性    

/// <summary>    
/// 内容类型,默认为"application/x-www-form-urlencoded"    
/// </summary>
public string ContentType    

/// <summary>    
/// Accept值,默认支持各种类型    
/// </summary>
public string Accept    

/// <summary>    
/// UserAgent,默认支持Mozilla/MSIE等    
/// </summary>
public string UserAgent    

/// <summary>    
/// Cookie容器    
/// </summary>
public CookieContainer CookieContainer    

/// <summary>    
/// 获取网页源码时使用的编码    
/// </summary>    
/// <value></value>
public Encoding Encoding    

/// <summary>    
/// 网络延时    
/// </summary>
public int NetworkDelay    

/// <summary>    
/// 最大尝试次数    
/// </summary>
public int MaxTry   

#endregion   

#region 构造函数    

/// <summary>    
/// 构造函数    
/// </summary>
public HttpHelper()    

/// <summary>    
/// 构造函数    
/// </summary>    
/// <param name="cc">指定CookieContainer的值</param>
public HttpHelper(CookieContainer cc)    

/// <summary>    
/// 构造函数    
/// </summary>    
/// <param name="contentType">内容类型</param>    
/// <param name="accept">Accept类型</param>    
/// <param name="userAgent">UserAgent内容</param>
public HttpHelper(string contentType, string accept, string userAgent)    

/// <summary>    
/// 构造函数    
/// </summary>    
/// <param name="cc">指定CookieContainer的值</param>    
/// <param name="contentType">内容类型</param>    
/// <param name="accept">Accept类型</param>    
/// <param name="userAgent">UserAgent内容</param>
public HttpHelper(CookieContainer cc, string contentType, string accept, string userAgent)   

#endregion   

#region 公共方法    

/// <summary>    
/// 获取指定页面的HTML代码    
/// </summary>    
/// <param name="url">指定页面的路径</param>    
/// <param name="cookieContainer">Cookie集合</param>    
/// <param name="postData">回发的数据</param>    
/// <param name="isPost">是否以post方式发送请求</param>    
/// <returns></returns>
public string GetHtml(string url, CookieContainer cookieContainer, string postData, bool isPost)    

/// <summary>    
/// 获取指定页面的HTML代码    
/// </summary>    
/// <param name="url">指定页面的路径</param>    
/// <param name="cookieContainer">Cookie集合对象</param>    
/// <param name="postData">回发的数据</param>    
/// <param name="isPost">是否以post方式发送请求</param>    
/// <param name="referer">页面引用</param>    
/// <returns></returns>
public string GetHtml(string url, CookieContainer cookieContainer, string postData, bool isPost, string referer)    

/// <summary>    
/// 获取指定页面的HTML代码    
/// </summary>    
/// <param name="url">指定页面的路径</param>    
/// <param name="cookieContainer">Cookie集合</param>    
/// <param name="reference">页面引用</param>    
/// <returns></returns>
public string GetHtml(string url, CookieContainer cookieContainer, string reference)    

/// <summary>    
/// 获取指定页面的HTML代码    
/// </summary>    
/// <param name="url">指定页面的路径</param>    
/// <returns></returns>
public string GetHtml(string url)    

/// <summary>    
/// 获取指定页面的HTML代码    
/// </summary>    
/// <param name="url">指定页面的路径</param>    
/// <param name="reference">页面引用</param>    
/// <returns></returns>
public string GetHtml(string url, string reference)    

/// <summary>    
/// 获取指定页面的HTML代码    
/// </summary>    
/// <param name="url">指定页面的路径</param>    
/// <param name="postData">回发的数据</param>    
/// <param name="isPost">是否以post方式发送请求</param>    
/// <returns></returns>
public string GetHtml(string url, string postData, bool isPost)    

/// <summary>    
/// 获取指定页面的Stream    
/// </summary>    
/// <param name="url">指定页面的路径</param>    
/// <param name="cookieContainer">Cookie集合对象</param>    
/// <returns></returns>
public Stream GetStream(string url, CookieContainer cookieContainer)    

/// <summary>    
/// 获取指定页面的Stream    
/// </summary>    
/// <param name="url">指定页面的路径</param>    
/// <param name="cookieContainer">Cookie对象</param>    
/// <param name="reference">页面引用</param>
public Stream GetStream(string url, CookieContainer cookieContainer, string reference)    

/// <summary>    
/// 根据Cookie字符串获取Cookie的集合    
/// </summary>    
/// <param name="cookieString">Cookie字符串</param>    
/// <returns></returns>
public CookieCollection GetCookieCollection(string cookieString)    

/// <summary>    
/// 获取HTML页面内容指定隐藏域Key的Value内容    
/// </summary>    
/// <param name="html">待操作的HTML页面内容</param>    
/// <param name="key">隐藏域的名称</param>    
/// <returns></returns>
public string GetHiddenKeyValue(string html, string key)    

/// <summary>    
/// 获取网页的编码格式    
/// </summary>    
/// <param name="url">网页地址</param>    
/// <returns></returns>
public string GetEncoding(string url)    

/// <summary>    
/// 判断URL是否有效    
/// </summary>    
/// <param name="url">待判断的URL,可以是网页以及图片链接等</param>    
/// <returns>200为正确,其余为大致网页错误代码</returns>
public int GetUrlError(string url)    

/// <summary>    
/// 移除Html标记    
/// </summary>
public string RemoveHtml(string content)    

/// <summary>    
/// 返回 HTML 字符串的编码结果    
/// </summary>    
/// <param name="inputData">字符串</param>    
/// <returns>编码结果</returns>
public static string HtmlEncode(string inputData)    

/// <summary>    
/// 返回 HTML 字符串的解码结果    
/// </summary>    
/// <param name="str">字符串</param>    
/// <returns>解码结果</returns>
public static string HtmlDecode(string str)   

#endregion

2)辅助类HttpHelper的使用例子代码如下所示,下面的是最为简单的获取页面内容的操作。

public void GetQQLog()    
{    
    string qq = this.txtQQ.Text;    
    string json = "";    

    string url = string.Format("http://b.qzone.qq.com/cgi-bin/blognew/blog_get_titlelist?direct=1&numperpage=100&uin={0}", qq);    
    HttpHelper helper = new HttpHelper();    
    helper.Encoding = Encoding.Default;    
    json = helper.GetHtml(url);    
    ..............    
}

实际操作可能更多的是要记录Cookie信息,方便下一次的调用,还有可能需要获取验证码等等,复杂一点的例子代码如下所示。

string uin = e.Argument.ToString();                
HttpHelper httpHelper = new HttpHelper();    
string refUrl = "http://qzone.qq.com/";    
string url = string.Format("http://ptlogin2.qq.com/check?uin={0}&appid=46000101&r=0.5454333601416937", uin);    
string checkHtml = httpHelper.GetHtml(url, Portal.gc.cookieZone, refUrl);    
//ptui_checkVC('1','56443c908b8be83dc4435e253e6b43ad99eab4fe0846930d');    
Regex re = new Regex("ptui_checkVC\\('\\d+','(.*?)'\\);", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);    
Match mc = re.Match(checkHtml);    
if (!mc.Success)    
{    
    return;    
}    

string vc_type = re.Matches(checkHtml)[0].Groups[1].Value;    
string imageUrl = string.Format("http://captcha.qq.com/getimage?aid=46000101&r=0.2758570793854393&uin={0}&vc_type={1}", uin, vc_type);    
using (Stream s = httpHelper.GetStream(imageUrl, Portal.gc.cookieZone))    
{    
    if (s == null)    
    {    
        MessageExUtil.ShowWarning("获取登陆码错误,请检查您的网络!");    
        return;    
    }    
    e.Result = Image.FromStream(s);    
}

上面的操作都是使用GET方式获取页面内容或者数据的,另外还有一种方式就是使用POST方式提交数据,并获取页面内容的,如下例子所示。

#region 构造提交参数    
StringBuilder sb = new StringBuilder();    
sb.AppendFormat("callCount=1");    
sb.AppendFormat("&page=/{0}/members/", urlName);    
sb.AppendFormat("&httpSessionId=");    
sb.AppendFormat("&scriptSessionId=D4DAC4AD9C3BF9B71C82802BDDBA0C25369");    
sb.AppendFormat("&c0-scriptName=CircleBean");    
sb.AppendFormat("&c0-methodName=getNewCircleUsers");    
sb.AppendFormat("&c0-id=0");//保留字符    
sb.AppendFormat("&c0-param0=number:{0}", circleId);//11    
sb.AppendFormat("&c0-param1=number:{0}", pageSize);//数量    
sb.AppendFormat("&c0-param2=number:{0}", pageSize * i);//0,30,60    
sb.AppendFormat("&c0-param3=boolean:true");    
sb.AppendFormat("&batchId={0}", i);    
i++;    

//callCount=1    
//page=/dnkxin/members/    
//httpSessionId=    
//scriptSessionId=D4DAC4AD9C3BF9B71C82802BDDBA0C25369    
//c0-scriptName=CircleBean    
//c0-methodName=getNewCircleUsers    
//c0-id=0    
//c0-param0=number:15057111            //(<body onload="MembersPage.init('15057111', '/style/pinkstar/','http://blog.163.com','')">)    
//c0-param1=number:10    
//c0-param2=number:0    
//c0-param3=boolean:true    
//batchId=0   
#endregion    

string content = "";    
try   
{    
    httpHelper.ContentType = "text/plain";    
    content = httpHelper.GetHtml(url, cookie, sb.ToString(), true, refUrl);//使用Post方式提交内容,并返回页面结果    
    re = new Regex(circleReg.ToString(), RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace);    
    mc = re.Match(content);    
}    
catch (Exception ex)    
{    
    LogTextHelper.WriteLine(ex.ToString());    
    break;    
}

下面是一些具体应用的例子截图,主要就是通过该辅助类获取、分析网页的处理结果。



3、管理文档服务器类,提供文件上传、下载、删除等功能辅助类 FileServerManage。
实现效果
1) 本辅助类主要是用来方便实现管理文档服务器类,提供文件上传、下载、删除等功能。 不过注意,由于IIS操作限制,可能一些后缀名的文件不支持访问。
2)FileServerManage 主要是通过IIS网站方式上传管理文件附件的,要使用该辅助类库的功能,需要为指定的目录(一般在文件服务器上一个目录)建立一个虚拟网站,指定端口等参数,该辅助类库的构造函数,使用的是虚拟网站的URL地址以及文件服务器的登录账号和密码(本机则使用本机的账号密码)。 3)另外使用IIS作为文件上传操作,除了需要用到当前机器的账号密码登录外,还需要在IIS管理中开通/启用WebDev的功能。

实现代码
1)辅助类提供的方法源码如下所示:

/// <summary>    
/// 构造函数    
/// </summary>
public FileServerManage()    

/// <summary>    
/// 构造函数    
/// </summary>    
/// <param name="url">指定URL地址</param>    
/// <param name="username">指定用户名</param>    
/// <param name="password">指定密码</param>
public FileServerManage(string url, string username, string password)    

/// <summary>    
/// 上传文件    
/// </summary>    
/// <param name="inputStream">流对象</param>    
/// <param name="fileName">保存文件名,可包含文件夹(test/test.txt)</param>    
/// <returns>bool[true:成功,false:失败]</returns>
public bool UploadFile(Stream inputStream, string fileName)    

/// <summary>    
/// 上传文件    
/// </summary>    
/// <param name="fileUrl">上传地址</param>    
/// <param name="fileName">上传文件名称,可包含文件夹(test/test.txt)</param>    
/// <returns>bool[true:成功,false:失败]</returns>
public bool UploadFile(string fileUrl, string fileName)    

/// <summary>    
/// 删除文件    
/// </summary>    
/// <param name="fileName">文件名称,可包含文件夹(test/test.txt)</param>    
/// <returns>bool[true:成功,false:失败]</returns>
public bool DeleteFile(string fileName)    

/// <summary>    
/// 判断文件是否存在    
/// </summary>    
/// <param name="fileName">文件名称,可包含文件夹(test/test.txt)</param>    
/// <returns>bool[true:存在,false:否]</returns>
public bool IsFileExist(string fileName)    

/// <summary>    
/// 通过HttpResponse方式读取文件,Web开发才可以使用    
/// </summary>    
/// <param name="newFileName">新文件名称,可包含文件夹(test/test.txt)</param>    
/// <param name="oldFileName">原文件名称</param>    
/// <returns></returns>
public string ReadFile(string newFileName, string oldFileName)    

/// <summary>    
/// 读取服务器文件到字节数据中    
/// </summary>    
/// <param name="fileName">文件名称,可包含文件夹(test/test.txt)</param>    
/// <returns></returns>
public byte[] ReadFileBytes(string fileName)

2)辅助类FileServerManage的使用例子代码如下所示

private void btnWebFile_Click(object sender, EventArgs e)    
{    
    WHC.OrderWater.Commons.Web.FileServerManage file = new WHC.OrderWater.Commons.Web.FileServerManage("http://192.168.1.30:8009", "administrator", "123456789");    
    try   
    {    
        //上传文件    
        bool first = false;    
        using (FileStream fs = new FileStream("C:\\test.txt", FileMode.Open))    
        {    
            first = file.UploadFile(fs, "test.txt");    
        }    

        //利用子目录上传,需要服务器手动创建目录    
        bool second = file.UploadFile("C:\\test.txt", "Bridge/test.txt");    

        MessageUtil.ShowTips(string.Format("第一次上传:{0} 第二次上传{1}", first, second));    

        byte[] fileBytes = file.ReadFileBytes("test.txt");    
        if (fileBytes != null)    
        {    
            MessageUtil.ShowTips(string.Format("File Bytes:{0}", fileBytes.Length));    
        }    

        //删除文件    
        first = file.DeleteFile("test.txt");    
        bool third = file.IsFileExist("Bridge/test.txt");    
        second = file.DeleteFile("Bridge/test.txt");     
        MessageUtil.ShowTips(string.Format("删除文件:{0}、{1} 文件存在:{2}", first, second, third));    

    }    
    catch (Exception ex)    
    {    
        MessageUtil.ShowError(ex.Message);    
    }    
}

4、网络相关操作辅助类 NetworkUtil
实现效果
1) 本辅助类主要是用来方便实现网络相关操作,可以对IP或者域名进行相互解析,基于Socket的TCP/UDP相关操作,检测本机是否联网等相关的网络操作。
实现代码
1)辅助类提供的方法源码如下所示:

/// <summary>    
/// 获取本地机器IP地址    
/// </summary>    
/// <returns></returns>
public static string GetLocalIP()    

/// <summary>    
/// 检查设置的IP地址是否正确,并返回正确的IP地址,无效IP地址返回"-1"。    
/// </summary>    
/// <param name="ip">设置的IP地址</param>    
/// <returns>非法IP 则返回 -1 </returns>
public static string GetValidIP(string ip)    

/// <summary>    
/// 检查设置的端口号是否正确,并返回正确的端口号,无效端口号返回-1。    
/// </summary>    
/// <param name="port">设置的端口号</param>
public static int GetValidPort(string port)    

/// <summary>    
/// 将字符串形式的IP地址转换成IPAddress对象    
/// </summary>    
/// <param name="ip">字符串形式的IP地址</param>
public static IPAddress StringToIPAddress(string ip)    

/// <summary>    
/// 获取本机的计算机名    
/// </summary>
public static string LocalHostName    

/// <summary>    
/// 获取本机的局域网IP    
/// </summary>
public static string LANIP    

/// <summary>    
/// 获取本机在Internet网络的广域网IP    
/// </summary>
public static string WANIP    

/// <summary>    
/// 获取远程客户机的IP地址    
/// </summary>    
/// <param name="clientSocket">客户端的socket对象</param>
public static string GetClientIP(Socket clientSocket)    

/// <summary>    
/// 创建一个IPEndPoint对象    
/// </summary>    
/// <param name="ip">IP地址</param>    
/// <param name="port">端口号</param>
public static IPEndPoint CreateIPEndPoint(string ip, int port)    

/// <summary>    
/// 创建一个自动分配IP和端口的TcpListener对象    
/// </summary>
public static TcpListener CreateTcpListener()    

/// <summary>    
/// 创建一个TcpListener对象    
/// </summary>    
/// <param name="ip">IP地址</param>    
/// <param name="port">端口</param>
public static TcpListener CreateTcpListener(string ip, int port)    

/// <summary>    
/// 创建一个基于TCP协议的Socket对象    
/// </summary>
public static Socket CreateTcpSocket()    

/// <summary>    
/// 创建一个基于UDP协议的Socket对象    
/// </summary>
public static Socket CreateUdpSocket()   

#region 获取TcpListener对象的本地终结点    
/// <summary>    
/// 获取TcpListener对象的本地终结点    
/// </summary>    
/// <param name="tcpListener">TcpListener对象</param>
public static IPEndPoint GetLocalPoint(TcpListener tcpListener)    

/// <summary>    
/// 获取TcpListener对象的本地终结点的IP地址    
/// </summary>    
/// <param name="tcpListener">TcpListener对象</param>
public static string GetLocalPoint_IP(TcpListener tcpListener)    

/// <summary>    
/// 获取TcpListener对象的本地终结点的端口号    
/// </summary>    
/// <param name="tcpListener">TcpListener对象</param>
public static int GetLocalPoint_Port(TcpListener tcpListener)   

#endregion   

#region 获取Socket对象的本地终结点    

/// <summary>    
/// 获取Socket对象的本地终结点    
/// </summary>    
/// <param name="socket">Socket对象</param>
public static IPEndPoint GetLocalPoint(Socket socket)    

/// <summary>    
/// 获取Socket对象的本地终结点的IP地址    
/// </summary>    
/// <param name="socket">Socket对象</param>
public static string GetLocalPoint_IP(Socket socket)    

/// <summary>    
/// 获取Socket对象的本地终结点的端口号    
/// </summary>    
/// <param name="socket">Socket对象</param>
public static int GetLocalPoint_Port(Socket socket)   

#endregion    

/// <summary>    
/// 绑定终结点    
/// </summary>    
/// <param name="socket">Socket对象</param>    
/// <param name="endPoint">要绑定的终结点</param>
public static void BindEndPoint(Socket socket, IPEndPoint endPoint)    

/// <summary>    
/// 绑定终结点    
/// </summary>    
/// <param name="socket">Socket对象</param>            
/// <param name="ip">服务器IP地址</param>    
/// <param name="port">服务器端口</param>
public static void BindEndPoint(Socket socket, string ip, int port)    

/// <summary>    
/// 指定Socket对象执行监听,默认允许的最大挂起连接数为100    
/// </summary>    
/// <param name="socket">执行监听的Socket对象</param>    
/// <param name="port">监听的端口号</param>
public static void StartListen(Socket socket, int port)    

/// <summary>    
/// 指定Socket对象执行监听    
/// </summary>    
/// <param name="socket">执行监听的Socket对象</param>    
/// <param name="port">监听的端口号</param>    
/// <param name="maxConnection">允许的最大挂起连接数</param>
public static void StartListen(Socket socket, int port, int maxConnection)    

/// <summary>    
/// 指定Socket对象执行监听    
/// </summary>    
/// <param name="socket">执行监听的Socket对象</param>    
/// <param name="ip">监听的IP地址</param>    
/// <param name="port">监听的端口号</param>    
/// <param name="maxConnection">允许的最大挂起连接数</param>
public static void StartListen(Socket socket, string ip, int port, int maxConnection)    

/// <summary>    
/// 连接到基于TCP协议的服务器,连接成功返回true,否则返回false    
/// </summary>    
/// <param name="socket">Socket对象</param>    
/// <param name="ip">服务器IP地址</param>    
/// <param name="port">服务器端口号</param>
public static bool Connect(Socket socket, string ip, int port)    

/// <summary>    
/// 以同步方式向指定的Socket对象发送消息    
/// </summary>    
/// <param name="socket">socket对象</param>    
/// <param name="msg">发送的消息</param>
public static void SendMsg(Socket socket, byte[] msg)    

/// <summary>    
/// 使用UTF8编码格式以同步方式向指定的Socket对象发送消息    
/// </summary>    
/// <param name="socket">socket对象</param>    
/// <param name="msg">发送的消息</param>
public static void SendMsg(Socket socket, string msg)    

/// <summary>    
/// 以同步方式接收消息    
/// </summary>    
/// <param name="socket">socket对象</param>    
/// <param name="buffer">接收消息的缓冲区</param>
public static void ReceiveMsg(Socket socket, byte[] buffer)    

/// <summary>    
/// 以同步方式接收消息,并转换为UTF8编码格式的字符串,使用5000字节的默认缓冲区接收。    
/// </summary>    
/// <param name="socket">socket对象</param>
public static string ReceiveMsg(Socket socket)    

/// <summary>    
/// 关闭基于Tcp协议的Socket对象    
/// </summary>    
/// <param name="socket">要关闭的Socket对象</param>
public static void Close(Socket socket)    


/// <summary>    
/// 检测本机是否联网    
/// </summary>    
/// <returns></returns>
public static bool IsConnectedInternet()    

/// <summary>    
/// 检测本机是否联网的连接属性    
/// </summary>
public static InternetConnectionStatesType CurrentState    

/// <summary>    
/// 检测本机是否联网(互联网)    
/// </summary>    
/// <returns></returns>
public static bool IsOnline()    

/// <summary>    
/// 转换主机域名DNS到IP地址    
/// </summary>    
/// <param name="hostname">主机域名DNS</param>    
/// <returns></returns>
public static string ConvertDnsToIp(string hostname)    

/// <summary>    
/// 转换主机IP地址到DNS域名    
/// </summary>    
/// <param name="ipAddress">主机IP地址</param>    
/// <returns></returns>
public static string ConvertIpToDns(string ipAddress)    

/// <summary>    
/// 根据IP端点获取主机名称    
/// </summary>    
/// <param name="ipEndPoint">IP端点</param>    
/// <returns></returns>
public static string GetHostName(IPEndPoint ipEndPoint)    

/// <summary>    
/// 根据主机IP地址对象获取主机名称    
/// </summary>    
/// <param name="ip">主机IP地址对象</param>    
/// <returns></returns>
public static string GetHostName(IPAddress ip)    

/// <summary>    
/// 根据主机IP获取主机名称    
/// </summary>    
/// <param name="hostIP">主机IP</param>    
/// <returns></returns>
public static string GetHostName(string hostIP)    

/// <summary>    
/// 得到一台机器的EndPoint端点    
/// </summary>    
/// <param name="entry">主机实体</param>    
/// <returns></returns>
public static EndPoint GetNetworkAddressEndPoing(IPHostEntry entry)    

/// <summary>    
/// 主机名是否存在    
/// </summary>    
/// <param name="host">主机名</param>    
/// <returns></returns>
public static bool IsHostAvailable(string host)    


/// <summary>    
/// 在主机名解析到一个IP主机实体    
/// </summary>    
/// <param name="hostname">主机名</param>    
/// <returns></returns>
public static IPHostEntry ResolveHost(string host)

2)辅助类NetworkUtil的使用例子代码如下所示。

private void btnNetWork_Click(object sender, EventArgs e)    
{    
    StringBuilder sb = new StringBuilder();    
    sb.AppendFormat("本机IP:{0} \r\n", NetworkUtil.GetLocalIP());    
    sb.AppendFormat("检测本机是否联网:{0} \r\n", NetworkUtil.IsConnectedInternet());    
    sb.AppendFormat("www.iqid.com域名IP:{0} \r\n", NetworkUtil.ConvertDnsToIp("www.iqidi.com"));    
    sb.AppendFormat("本机LocalHostName:{0}  \r\n", NetworkUtil.LocalHostName);    
    sb.AppendFormat("本机局域网IP:{0}  \r\n", NetworkUtil.LANIP);    
    sb.AppendFormat("本机广域网IP:{0}  \r\n", NetworkUtil.WANIP);    

    Socket socket = NetworkUtil.CreateTcpSocket();    
    Socket udpsocket = NetworkUtil.CreateUdpSocket();    
    TcpListener listen = NetworkUtil.CreateTcpListener("127.0.0.1", 9900);    
    listen.Start(100);    

    MessageUtil.ShowTips(sb.ToString());    
}

5、IE代理设置辅助类 ProxyHelper。
实现效果
1) 本辅助类主要是用来方便实现IE代理设置操作。
实现代码
1)辅助类提供的方法源码如下所示:

/// <summary>    
/// IE代理设置辅助类    
/// </summary>
public class ProxyHelper    
{   
    #region IE代理设置    

    /// <summary>    
    /// 让IE支持WAP    
    /// </summary>
    public static void SetIESupportWap()    

    /// <summary>    
    /// 设置代理    
    /// </summary>    
    /// <param name="ProxyServer">代理服务器</param>    
    /// <param name="EnableProxy">设置代理可用</param>    
    /// <returns></returns>
    public static string SetIEProxy(string ProxyServer, int EnableProxy)   


    #endregion   

    #region 其他操作    

    /// <summary>    
    /// 测试代理配置    
    /// </summary>    
    /// <param name="setting">代理信息</param>    
    /// <param name="te">测试信息</param>
    public static bool TestProxy(ProxySettingEntity setting, TestEntity te)    

    /// <summary>    
    /// 代理设置    
    /// </summary>    
    /// <param name="request">Web请求</param>    
    /// <param name="Proxy">代理设置</param>
    public static void SetProxySetting(WebRequest request, ProxySettingEntity Proxy)   

    #endregion    
}    

/// <summary>    
/// 测试信息    
/// </summary>
[Serializable]    
public class TestEntity    
{    
    /// <summary>    
    /// 测试网站地址    
    /// </summary>
    public string TestUrl    

    /// <summary>    
    /// 测试网站Title    
    /// </summary>
    public string TestWebTitle    

    /// <summary>    
    /// Web编码    
    /// </summary>
    public string TestWebEncoding    
}    

/// <summary>    
/// 代理设置    
/// </summary>
[Serializable]    
public class ProxySettingEntity    
{    
    /// <summary>    
    /// 编号    
    /// </summary>
    public int Id    

    /// <summary>    
    /// 代理服务器IP    
    /// </summary>
    public string Ip    

    /// <summary>    
    /// 代理服务器端口    
    /// </summary>
    public int Port    

    /// <summary>    
    /// 代理用户名    
    /// </summary>
    public string UserName    

    /// <summary>    
    /// 代理密码    
    /// </summary>
    public string Password    

    /// <summary>    
    /// 代理类型    
    /// </summary>
    public int ProxyType    
}

2)辅助类ProxyHelper的使用例子代码如下所示

CHM帮助文档持续更新中,统一下载地址是: http://www.iqidi.com/download/commonshelp.rar
公用类库DLL+XML注释文件下载地址是:http://files.cnblogs.com/wuhuacong/WHC.OrderWater.Commons.rar