文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>C#一些常用函数的整理

C#一些常用函数的整理

时间:2010-09-02  来源:狂想曲123

  一、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>

  /// 发送邮件www.qichepeijian.com

  /// </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("发送邮件出错!\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;

  }

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载