http://www.cnblogs.com/devsdk/archive/2009/04/11/1433475.html
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
namespace DevSDK.Partners.DNS_COM_CN.SMS
{
/// <summary>
/// WebHttpRequest 基礎類
/// </summary>
public class dsWebHttpRequest
{
/// <summary>
/// WebHttpRequest 基礎類
/// </summary>
/// <param name="webMethod">提交方式 POST,GET</param>
/// <param name="sURL">提交路徑</param>
/// <param name="webPostData">參數字串</param>
/// <returns>返回HTML代碼</returns>
internal string WebHttpRequest(string webMethod, string sURL, string webPostData)
{
byte[] sData = Encoding.Default.GetBytes(webPostData);
string fString = "";
#region "WEB執行個體初始化"
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
Uri webURL = new Uri(sURL);
Stream webStream = null;
StreamReader webReader = null;
#endregion
try
{
webRequest = (HttpWebRequest)WebRequest.Create(webURL);
webRequest.Method = webMethod;
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = sData.Length;
try
{
webStream = webRequest.GetRequestStream();
webStream.Write(sData, 0, sData.Length);
webStream.Close();
}
catch (IOException ioEx)
{
throw ioEx;
}
finally
{
webStream.Close(); webStream.Dispose(); //清空執行個體
}
webResponse = (HttpWebResponse)webRequest.GetResponse();
try
{
webReader = new StreamReader(webResponse.GetResponseStream(), Encoding.Default);
fString = webReader.ReadToEnd();
return fString;
}
catch (IOException ioEx)
{
throw ioEx;
}
finally
{
webReader.Dispose();
}
}
catch (HttpListenerException httpEx)
{
throw httpEx;
}
finally
{
#region "清空執行個體對象"
webStream.Dispose();
webReader.Dispose();
#endregion
}
}
/// <summary>
/// 安全字串過濾
/// </summary>
/// <param name="fString">非安全字串</param>
/// <returns>安全字串</returns>
internal string HTMLSQLEncodes(string fString)
{
string tempStr = "";
for (int i = 0; i < fString.Length; i++)
{
if (char.IsNumber(fString, i))
{
tempStr += fString.Substring(i, 1);
}
}
return tempStr;
}
/// <summary>
/// 32位MD5加密
/// </summary>
/// <param name="sString">待加密字串</param>
/// <returns>加密後的字串</returns>
internal string MD5(string sString)
{
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(sString);
System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(data);
return BitConverter.ToString(result).Replace("-", "");
}
}
}