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("-", "");
}
}
}