您现在的位置是:首页 >

EXCEL基础函数 C#一些常用函数的整理

火烧 2022-01-19 03:11:27 1056
C#一些常用函数的整理   一 C#客户端通过POST或GET向指定的网址发送数据  发送请求  /// lt ummary gt   /// 发送请求  /// lt / ummary gt   /

C#一些常用函数的整理  

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

  发送请求

  /// <summary>

  /// 发送请求

  /// </summary>

  /// <param name= url >网址</param>

  /// <param name= parameter >要发送的值 如 abc= &bcd= </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 = ;

  //hwrq CookieContainer = cc;

  hwrq Method = method;

  byte[] postData = System Text Encoding UTF GetBytes(parameter);

  hwrq ContentType = application/x form urlencoded ;

  hwrq ContentLength = postData Length;

  Stream writeStream = hwrq GetRequestStream();

  writeStream Write(postData 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)

  {

EXCEL基础函数 C#一些常用函数的整理

  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 >邮件内容 支持</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)[ ] ToString();

  client UseDefaultCredentials = false;

  client Credentials = new System Net NeorkCredential(strFrom strFromPass);

  //星号改成自己邮箱的密码

  client DeliveryMethod = SmtpDeliveryMethod Neork;

  message = new MailMessage(strFrom strto);

  message Subject = strSubject;

  message Body = strBody;

  message BodyEncoding = System Text Encoding UTF ;

  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 UTF ))

  {

  sw Write( 发送邮件出错!n + ex Message + n + ex StackTrace + n===========================n );

  }

  return false;

  }

  finally

  {

  if (message != null)

  {

  foreach (Attachment forData in message Attachments)

  forData Dispose();

  message Attachments Clear();

  message Dispose();

  }

  }

  return true;

lishixinzhi/Article/program/net/201311/11601  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码