一个简单的C# XMLHTTP调用

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

转载于:https://www.cnblogs.com/hanwater/archive/2009/06/30/1514104.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值