C#一些常用函数的整理

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

  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值