首先把“Interop.MSXML2.dll”,复制到项目的Bin目录下
XmlHttp.cs

using System;
using MSXML2;

/// <summary>
/// XmlHttp 的摘要说明
/// </summary>
public class XmlHttp : IDisposable
{
    /// <summary>
    /// 创建 XmlHttp
    /// </summary>
    public XMLHTTPClass XmlHttps = new XMLHTTPClass();
    private bool readyDispose = false;

    /// <summary>
    /// 发送方式
    /// </summary>
    public enum SendMethod : int
    {
        POST, GET
    }

    /// <summary>
    /// 数据包
    /// </summary>
    public struct Param
    {
        public string Url;
        public string Parameters;
        public string Uid;
        public string Pwd;
    }

    /// <summary>
    /// 发送数据
    /// </summary>
    /// <param name="method">发送方式</param>
    /// <param name="p">数据包</param>
    /// <returns></returns>
    public virtual string SendCommond(SendMethod method, Param p)
    {
        if (p.Url == null || p.Url == "")
        {
            return null;
        }
        if (method == SendMethod.POST)
        {
            try
            {
                XmlHttps.open("POST", p.Url, false, p.Uid, p.Pwd);
                XmlHttps.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                XmlHttps.send(p.Parameters.Replace("?", ""));
                if (XmlHttps.status == 200)
                {
                    return System.Text.Encoding.Default.GetString((byte[])XmlHttps.responseBody);
                }
                else
                {
                    return XmlHttps.status.ToString();
                }
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }
        else if (method == SendMethod.GET)
        {
            try
            {
                XmlHttps.open("GET", p.Url + "?" + p.Parameters.Replace("?", ""), false, p.Uid, p.Pwd);
                XmlHttps.send(null);
                if (XmlHttps.status == 200)
                {
                    return System.Text.Encoding.Default.GetString((byte[])XmlHttps.responseBody);
                }
                else
                {
                    return XmlHttps.status.ToString();
                }
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }
        return null;
    }

    /// <summary>
    /// 类接口
    /// </summary>
    /// <param name="isDisposing"></param>
    protected virtual void Dispose(bool isDisposing)
    {
        if (readyDispose)
        {
            return;
        }
        if (isDisposing)
        {
            if (XmlHttps != null)
            {
                XmlHttps = null;
            }
        }
        readyDispose = true;
    }

    /// <summary>
    /// 释放内存
    /// </summary>
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }
}




AJAXProxy.aspx

using System;
using System.Text;

public partial class AJAXProxy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Encoding utf8 = Encoding.GetEncoding("utf-8");
            Response.ContentEncoding = utf8;
            Response.ContentType = "text/xml";
            XmlHttp XmlHttps = new XmlHttp();
            XmlHttp.Param Param = new XmlHttp.Param();
            Param.Url = "http://" + sUrl;
            Param.Parameters = Server.UrlEncode(sParam).Replace("%3d", "=").Replace("%24", "$").Replace("$", "&") + "&time=" + Convert.ToString(System.DateTime.Now);
            Param.Uid = "";
            Param.Pwd = "";
            XmlHttp.SendMethod Method = XmlHttp.SendMethod.GET;
            if (sMethod != "GET")
            {
                Method = XmlHttp.SendMethod.POST;
            }
            else
            {
                Method = XmlHttp.SendMethod.GET;
            }
            string result = XmlHttps.SendCommond(Method, Param);
            XmlHttps.Dispose();
            Response.Write(result);
        }
    }

    /// <summary>
    /// 方式
    /// </summary>
    private string sMethod
    {
        get
        {
            if (Request["sMethod"] != null)
            {
                return Request["sMethod"].ToString();
            }
            return "GET";
        }
    }

    /// <summary>
    /// 网址
    /// </summary>
    private string sUrl
    {
        get
        {
            if (Request["sUrl"] != null)
            {
                return Request["sUrl"].ToString();
            }
            return null;
        }
    }

    /// <summary>
    /// 参数
    /// </summary>
    private string sParam
    {
        get
        {
            if (Request["sParam"] != null)
            {
                return Request["sParam"].ToString();
            }
            return "";
        }
    }
}



Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AjaxProxy</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script type="text/javascript" language="javascript">
function createXMLHttps()
{
    var ret = null;
    try
    {
        ret = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e)
    {
        try
        {
            ret = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (ee)
        {
            ret = null;
        }
    }
    if ( ! ret && typeof XMLHttpRequest != 'undefined')
    ret = new XMLHttpRequest();
    return ret;
}

function getDataTime()
{
    var strDate, strTime;
    strDate = new Date();
    strTime = strDate.getTime();
    return strTime;
}

function $(obj)
{
    return document.getElementById(obj);
}

function SendCommond()
{
    var xmlhttp = createXMLHttps();
    var time = getDataTime();
    var sParam = escape($("sParam").value).replace(/%3D/g, "=").replace(/%24/g, "$");
    var sendTo = "AJAXProxy.aspx?sMethod=GET&sUrl=" + $("sUrl").value + "&sParam=" + sParam + "&time=" + time;
    xmlhttp.open("GET", sendTo);
    xmlhttp.onreadystatechange = function()
    {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            var result = xmlhttp.responseText;
            alert((result));
        }
    }
    xmlhttp.send(null);
}
</script>
</head>
<body>
    网址:<input id="sUrl" type="text" value="http://www.youthfly.net/Ajax.aspx" /><br />
    参数:<input id="sParam" type="text" value="Key=参数A$Value=参数B" /><br />
    <input type="button" value=" 发送数据 " onclick="SendCommond();" /><br />
    <br />
</body>
</html>




Ajax.aspx

using System;
using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            Response.ContentEncoding = gb2312;
            Response.ContentType = "text/xml";
            string content = "Key:" + sKey + "<br />Value:" + sValue;
            if (!Exists(Server.MapPath("FileTest.txt")))
            {
                CreateFile(Server.MapPath("FileTest.txt"), content);
            }
            Response.Write(ReadFile(Server.MapPath("FileTest.txt")));
        }
    }

    /// <summary>
    /// 创建文件
    /// </summary>
    /// <param name="filename"></param>
    /// <param name="content"></param>
    private void CreateFile(string filename, string content)
    {
        try
        {
            using (StreamWriter sw = new StreamWriter(filename, false, System.Text.Encoding.GetEncoding("GB2312")))
            {
                sw.Write(content);
                sw.Close();
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.ToString());
        }
    }

    /// <summary>
    /// 读取文件
    /// </summary>
    /// <param name="filename"></param>
    /// <returns></returns>
    private string ReadFile(string filename)
    {
        try
        {
            using (StreamReader sr = new StreamReader(filename, System.Text.Encoding.GetEncoding("GB2312")))
            {
                string temp = sr.ReadToEnd();
                sr.Close();
                return temp;
            }
        }
        catch (Exception e)
        {
            throw new Exception(e.ToString()); ;
        }
    }

    /// <summary>
    /// 判断文件
    /// </summary>
    /// <param name="filepath"></param>
    /// <returns></returns>
    private bool Exists(string filepath)
    {
        if (File.Exists(filepath))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// 参数A
    /// </summary>
    private string sKey
    {
        get
        {
            if (Request["key"] != null)
            {
                return Request["key"].ToString();
            }
            return "空值";
        }
    }

    /// <summary>
    /// 参数B
    /// </summary>
    private string sValue
    {
        get
        {
            if (Request["value"] != null)
            {
                return Request["value"].ToString();
            }
            return "空值";
        }
    }
}




以上为主要核心代码, Aajx的相关代码请参考XmlHttp中文参考

AJAXProxy.rar
AJAXServer.rar

2008/03/14