using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text;
using V5Shop.Common;
using V5Shop.Data;
using System.Net;

public partial class member_Api_Wdgj_ProductApi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = GetPostValue("mType");
        string code = GetPostValue("uCode");
        string app = "wdgj";
        string verifyCode = MD5("v5shopv5mall");
        if (action == "mTest" && code == "v5shop")
        {
            OutSuccessResult();
            return;
        }

        //如果接入码不一致则退出
        if (code != "v5shop")
        {
            OutFaiedResult("接入码不一致");
            return;
        }
        else
        {
            GetUrlHtml(GetTrueUrl().Replace("member/api/", "") + "member/InfoCenter/InfoCenterApi.aspx?ic_typ=webrequest&ic_key=Product&action=" + action + "&ic_code=" + verifyCode + "&ic_app=" + app + "&" + collectQuery());
        }
    }

    /// <summary> 
    /// MD5 加密函数 
    /// </summary> 
    /// <param name="str"></param> 
    /// <param name="code"></param> 
    /// <returns></returns> 
    public static string MD5(string str)
    {
        return FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
    }

    /// <summary>
    /// 获取真实URL地址
    /// </summary>
    /// <returns></returns>
    public static string GetTrueUrl()
    {
        string TrueUrl = string.Empty;
        string TheUrl = HttpContext.Current.Request.Url.ToString();
        string[] UrlArray = TheUrl.Split('?')[0].Split('/');

        TrueUrl = TheUrl.Split('?')[0].Replace(UrlArray[UrlArray.Length - 1], "");

        return TrueUrl;
    }

    /// <summary>
    /// 输出成功的页面结果
    /// </summary>
    private void OutSuccessResult()
    {
        Response.Write("<rsp><result>OK</result></rsp>");
        Response.End();
    }

    /// <summary>
    /// 输出失败的页面结果
    /// </summary>
    private void OutFaiedResult(string result)
    {
        Response.Write("<rsp><result>Reject</result><cause>" + result + "</cause></rsp>");
        Response.End();
    }

    /// <summary>
    /// 获取POST提交的值
    /// </summary>
    /// <param name="key"></param>
    private string GetPostValue(string key)
    {
        return Request.Form[key] == null ? "" : Request.Form[key];
    }

    /// <summary>
    /// 合并传递的QUERYSTRING字符串
    /// </summary>
    /// <returns></returns>
    private string collectQuery()
    {
        StringBuilder queryString = new StringBuilder();
        int i = 0;

        foreach (string query in Request.Form)
        {
            if (i == 0)
            {
                queryString.Append(query + "=" + HttpUtility.HtmlEncode(Request.Form[query]));
                i = 1;
            }
            else
            {
                queryString.Append("&" + query + "=" + HttpUtility.HtmlEncode(Request.Form[query]));
            }
        }

        return queryString.ToString();
    }

    /// <summary>
    /// 读取页面生成静态页面
    /// </summary>
    /// <param name="url">地址</param>
    /// <param name="fna"></param>
    /// <returns></returns>
    private string GetUrlHtml(string url)
    {
        bool ok;
        //准备生成
        string strHtml;
        StreamReader sr = null; //用来读取流
        StreamWriter sw = null; //用来写文件
        Encoding code = Encoding.GetEncoding("utf-8"); //定义编码 
        //构造web请求，发送请求，获取响应
        WebRequest HttpWebRequest = null;
        WebResponse HttpWebResponse = null;
        HttpWebRequest = WebRequest.Create(url);
        try
        {
            HttpWebResponse = HttpWebRequest.GetResponse();
            //获得流
            sr = new StreamReader(HttpWebResponse.GetResponseStream(), code);
            strHtml = sr.ReadToEnd();
        }
        catch (Exception ex)
        {
            strHtml = "";
        }

        return strHtml;
    }
}
