一個簡單的C# XMLHTTP調用

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.