httprequest无法在服务器上运行

时间:2013-10-23 23:10:16

标签: c# asp.net azure httprequest

我有一个页面调用数据收集服务来跟踪访问,然后重定向到另一个页面。我在ASPX页面上有以下代码,它在我的DEV环境(localhost)上完美运行,我使用Fiddler跟踪HTTPRequest

但是,当我将页面部署到登台服务器时,我看不到该请求被触发。我尝试将其部署到Azure Virtual MachineAzure网站。任何人都知道为什么这会对我的本地环境起作用而不是现场环境?两个版本的.NET都是相同的。

<%@ Import namespace="System.Collections" %>
<%@ Import namespace="System.IO" %>
<%@ Import namespace="System.Collections.Generic" %>
<%@ Import namespace="System.Net" %>
<%@ Import namespace="System.Reflection" %>
<%@ Import namespace="System.Text" %>
<%@ Import namespace="System.Web" %>
<%@ Import namespace="System.Web.Caching" %>
<%@ Import namespace="System.Diagnostics" %>
<%@ Page Language="C#" %>
<script runat="server">

public class WebTrendsGhostPage
    {
        /// <summary>
        /// 1. Composes the URIs needed to access the Data Collection API
        /// 2. Encodes the POST body containing customer data to send to the Data Collection API
        /// 3. Calls the Data Collection API to obtain a visitor id (/ids.svc)
        /// 4. Calls the Data Collection API to submit customer data (/events.svc)
        /// </summary>

        #region Parameters

        // turn verbose information on (debugging only)
        static bool dcsverbose = false;

        // customer-specific DCSID (get from WebTrends)
        static String dcsid = "XXXXXXXX"; //DCS ID goes here

        // hostname portion of the DC API url
        static String hostname = "msdc.webtrends.com"; //DC Domain goes here

        // protocol portion of the DC API url
        static String protocol = "http";

        // query parameter portion of the DC API url
        static String query_params = dcsverbose ? "?dcsverbose=true" : "";

        // version of the DC API
        static String apiversion = "v1";

        #endregion Parameters

        #region Properties

        public string ModuleName
        {
            get { return "WebTrendsRSS"; }
        }
        #endregion Properties

        #region Events

        public WebTrendsGhostPage()
        {
            HttpContext context = HttpContext.Current;
            PostWebTrends(context);
        }

        #endregion Events

        #region Methods

        protected void PostWebTrends(HttpContext context)
        {
            // compose urls
            String id_url = String.Format("{0}://{1}/{2}/{3}/ids.svc{4}", protocol, hostname, apiversion, dcsid, query_params);
            String event_url = String.Format("{0}://{1}/{2}/{3}/events.svc{4}", protocol, hostname, apiversion, dcsid, query_params);

            // get visitor identifier
            String id = PostUrl(id_url, null);

            // initialize entity-body
            Dictionary<String, String> body = new Dictionary<String, String>();

            body["dcsuri"] = Encode(context, "context here");        
            body["dcscip"] = Encode(context,"base url here");
            body["WT.ti"] = Encode(context,"title goes here");
            body["WT.dl"] = "5";

            PostUrl(event_url, body);
        }

        protected string Encode(HttpContext context, string webText)
        {
            return context.Server.UrlEncode(webText);
        }

        protected String PostUrl(String url, Dictionary<String, String> bodydata)
        {
            StringBuilder bodystr = new StringBuilder();

            if (bodydata != null)
            {
                foreach (KeyValuePair<string, string> kvp in bodydata)
                {
                    bodystr.Append(String.Format("{0}={1}&", kvp.Key, kvp.Value));
                }
            }

            String body = bodystr.ToString();

            // create request
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
            //myReq.UserAgent = "Mozilla/4.0+ (compatible; MSIE 7.0; Windows NT 5.1)";
            //myReq.UserAgent = CSContext.Current.Context.Request.UserAgent;
            myReq.Method = "POST";
            myReq.ContentType = "application/x-www-form-urlencoded";
            myReq.ContentLength = body.Length;
            String responsebody = "";
            // send request
            try
            {
                StreamWriter myReqStreamWriter = new StreamWriter(myReq.GetRequestStream());
                myReqStreamWriter.Write(body);
                myReqStreamWriter.Close();

                // read response
                HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
                StreamReader readStream = new StreamReader(response.GetResponseStream());
                responsebody = readStream.ReadToEnd();
                readStream.Close();
            }
            catch (WebException e)
            {
                // nothing we can do here b/c we don't want to interrupt the driect from happening
            }

            return responsebody;
        }

        #endregion Methods

    }

protected void Page_Load(object sender, EventArgs e)
{
    try {
    new WebTrendsGhostPage();
    }
    catch (Exception e) {
       Response.Write(e.ToString());
    }
}
</script>

0 个答案:

没有答案