• 一些常用函数的整理


    一、C#客户端通过POST或GET向指定的网址发送数据
     

    发送请求
    /// <summary>
    /// 发送请求
    /// </summary>

    /// <param name="url">网址</param>
    /// <param name="parameter">要发送的值。如:abc=4&bcd=5</param>
    /// <param name="method">发送的方式,“POST”还是“GET”</param>
    /// <returns>返回的结果</returns>
    public static string RequestUrl(string url, string parameter, string method)
    {
        
    try

        {
            HttpWebRequest hwrq 
    = null;
            
    if (method == "POST"
    )
            {
                hwrq 
    =
     (HttpWebRequest)HttpWebRequest.Create(url);
                hwrq.KeepAlive 
    = false
    ;
                hwrq.ReadWriteTimeout 
    = 10000
    ;
                
    //hwrq.CookieContainer = cc;

                hwrq.Method = method;
                
    byte[] postData =
     System.Text.Encoding.UTF8.GetBytes(parameter);
                hwrq.ContentType 
    = "application/x-www-form-urlencoded"
    ;
                hwrq.ContentLength 
    =
     postData.Length;
                Stream writeStream 
    =
     hwrq.GetRequestStream();
                writeStream.Write(postData, 
    0
    , postData.Length);
                writeStream.Close();
            }
            
    else if (method == "GET"
    )
            {
                hwrq 
    = (HttpWebRequest)HttpWebRequest.Create(url + "?" +
     System.Web.HttpUtility.UrlEncode(parameter));
                hwrq.KeepAlive 
    = false
    ;
                
    //hwrq.CookieContainer = cc;

                hwrq.Method = method;
            }

            
    if (hwrq != null
    )
            {
                HttpWebResponse hwrp 
    =
     (HttpWebResponse)hwrq.GetResponse();
                
    //return hwrp.ResponseUri.AbsoluteUri;

                StreamReader sr = new StreamReader(hwrp.GetResponseStream(), Encoding.Default);
                
    return
     sr.ReadToEnd();
            }
        }
        
    catch
     (Exception ex)
        {
            
    throw
     ex;
        }
        
    return null
    ;
    }


    二、邮件发送函数
     

    邮件发送
    /// <summary>
    /// 发送邮件
    /// </summary>

    /// <param name="strSmtpServer">Smtp地址</param>
    /// <param name="strFrom">发送方的邮件地址</param>
    /// <param name="strFromPass">发送方的邮件密码</param>
    /// <param name="strto">接受方的邮件地址</param>
    /// <param name="strSubject">邮件主题</param>
    /// <param name="strBody">邮件内容,支持html</param>
    /// <param name="Attachments">附件列表</param>
    /// <returns>成功与否</returns>
    public static bool SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody, string[] Attachments)
    {
        System.Net.Mail.SmtpClient client 
    = null
    ;
        System.Net.Mail.MailMessage message 
    = null
    ;
        
    try

        {
            client 
    = new SmtpClient();
            client.Host 
    = System.Net.Dns.GetHostAddresses(strSmtpServer)[0
    ].ToString();
            client.UseDefaultCredentials 
    = false
    ;
            client.Credentials 
    = new
     System.Net.NetworkCredential(strFrom, strFromPass);
            
    //星号改成自己邮箱的密码

            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            message 
    = new
     MailMessage(strFrom, strto);
            message.Subject 
    =
     strSubject;
            message.Body 
    =
     strBody;
            message.BodyEncoding 
    =
     System.Text.Encoding.UTF8;
            message.IsBodyHtml 
    = true
    ;

            
    //添加附件

            foreach (string forStr in Attachments)
            {
                Attachment data 
    = new
     Attachment(forStr, System.Net.Mime.MediaTypeNames.Application.Octet);
                message.Attachments.Add(data);
            }

            client.Send(message);
        }
        
    catch
     (Exception ex)
        {
            
    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Directory.GetCurrentDirectory().TrimEnd('\'+ "\Log.txt"true
    , Encoding.UTF8))
            {
                sw.Write(
    "发送邮件出错! " + ex.Message + " " + ex.StackTrace + " =========================== "
    );
            }
            
    return false
    ;
        }
        
    finally

        {
            
    if (message != null)
            {
                
    foreach (Attachment forData in
     message.Attachments)
                    forData.Dispose();
                message.Attachments.Clear();
                message.Dispose();
            }
        }
        
    return true
    ;
    }
  • 相关阅读:
    再学 GDI+[91]: TGPImage(11) 转灰度图像
    再学 GDI+[90]: TGPImage(10) 获取图像的调色板信息
    给 Memo 排序的函数
    再学 GDI+[97]: TGPImage(17) 获取 GDI+ 所支持的可编码、可解码的图像格式
    再学 GDI+[94]: TGPImage(14) 增减图像的红、绿、蓝三色的成分
    上周热点回顾(10.2611.1)
    博客园上海俱乐部Windows 7社区发布活动的奖品
    顶吧!顶出今日头条
    博客园电子期刊2009年10月刊发布啦
    对于近期社区问题的一点想法
  • 原文地址:https://www.cnblogs.com/wwwzzg168/p/3569121.html
Copyright © 2020-2023  润新知