熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#一些常用函數的整理

2022-06-13   來源: .NET編程 

  一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)HttpWebRequestCreate(url);

  hwrqKeepAlive = false;

  hwrqReadWriteTimeout = ;

  //hwrqCookieContainer = cc;

  hwrqMethod = method;

  byte[] postData = SystemTextEncodingUTFGetBytes(parameter);

  hwrqContentType = application/xwwwformurlencoded;

  hwrqContentLength = postDataLength;

  Stream writeStream = hwrqGetRequestStream();

  writeStreamWrite(postData postDataLength);

  writeStreamClose();

  }

  else if (method == GET)

  {

  hwrq = (HttpWebRequest)HttpWebRequestCreate(url + ? + SystemWebHttpUtilityUrlEncode(parameter));

  hwrqKeepAlive = false;

  //hwrqCookieContainer = cc;

  hwrqMethod = method;

  }

  if (hwrq != null)

  {

  HttpWebResponse hwrp = (HttpWebResponse)hwrqGetResponse();

  //return hwrpResponseUriAbsoluteUri;

  StreamReader sr = new StreamReader(hwrpGetResponseStream() EncodingDefault);

  return srReadToEnd();

  }

  }

  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)

  {

  SystemNetMailSmtpClient client = null;

  SystemNetMailMailMessage message = null;

  try

  {

  client = new SmtpClient();

  clientHost = SystemNetDnsGetHostAddresses(strSmtpServer)[]ToString();

  clientUseDefaultCredentials = false;

  clientCredentials = new SystemNetNetworkCredential(strFrom strFromPass);

  //星號改成自己郵箱的密碼

  clientDeliveryMethod = SmtpDeliveryMethodNetwork;

  message = new MailMessage(strFrom strto);

  messageSubject = strSubject;

  messageBody = strBody;

  messageBodyEncoding = SystemTextEncodingUTF;

  messageIsBodyHtml = true;

  //添加附件

  foreach (string forStr in Attachments)

  {

  Attachment data = new Attachment(forStr SystemNetMimeMediaTypeNamesApplicationOctet);

  messageAttachmentsAdd(data);

  }

  clientSend(message);

  }

  catch (Exception ex)

  {

  using (SystemIOStreamWriter sw = new SystemIOStreamWriter(DirectoryGetCurrentDirectory()TrimEnd(\\) + \\Logtxt true EncodingUTF))

  {

  swWrite(發送郵件出錯!\n + exMessage + \n + exStackTrace + \n===========================\n);

  }

  return false;

  }

  finally

  {

  if (message != null)

  {

  foreach (Attachment forData in messageAttachments)

  forDataDispose();

  messageAttachmentsClear();

  messageDispose();

  }

  }

  return true;

  }


From:http://tw.wingwit.com/Article/program/net/201311/11601.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.