c#

位置:IT落伍者 >> c# >> 浏览文章

C#一些常用函数的整理


发布日期:2019年12月23日
 
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)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;

}

               

上一篇:利用.NET FileStreams将DTD插入XML文件中

下一篇:如何在Visual Basic中使用导入API