代码之家  ›  专栏  ›  技术社区  ›  pearl67

在C中调用“DataWarehouseGetReportData”时发生API异常#

  •  0
  • pearl67  · 技术社区  · 11 年前

    我正在使用 Omniture 下载报告的api。当我使用检查状态时,报告已完成 DataWarehouseCheckRequest 方法现在,当我尝试使用 DataWarehouseGetReportData 方法,我得到

    CommunicationException Error in deserializing body of reply message for operation 'DataWarehouseGetReportData

    内部异常表示

    The specified type was not recognized: name='data_warehouse_report_row', namespace='http://www.omniture.com/', at <rows xmlns=''>

    我对C#和API都是新手。不知道如何解决这个问题。请帮忙。

    谢谢

    1 回复  |  直到 11 年前
        1
  •  0
  •   Chris de Groot    11 年前

    当您想要下载DW报告时,最好的选择是通过http下载。这是标准的方式,效率更高。

    CheckRequest的响应包含DataURL。使用它下载数据。

    下面是我在一个几乎相同的API(合作伙伴与您的企业API)中使用的一些c#示例代码(请注意,我也不是c#专家,因此您需要对此进行代码审查)。

            HttpWebResponse statusResponse = null;
            string response = "";
            StringBuilder sbUrl = new StringBuilder(dwrq.data_url);   // hardcode to variable "rest_url" for testing.
            HttpWebRequest omniRequest = (HttpWebRequest)WebRequest.Create(sbUrl.ToString());
            string timecreated = generateTimestamp();
            string nonce = generateNonce();
            string digest = getBase64Digest(nonce + timecreated + secret);
            nonce = base64Encode(nonce);
            omniRequest.Headers.Add("X-WSSE: UsernameToken Username=\"" + username + "\", PasswordDigest=\"" + digest + "\", Nonce=\"" + nonce + "\", Created=\"" + timecreated + "\"");
            omniRequest.Method = "GET";  // Switched from POST as GET is the right HTTP verb in this case
    
            try
            {
                statusResponse = (HttpWebResponse)omniRequest.GetResponse();
                using (Stream receiveStream = statusResponse.GetResponseStream())
                {
                    using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
                    {
                        response = readStream.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            Console.WriteLine("Response is a TAB delimeted CSV structure. Printing to screen.");
            Console.WriteLine(response);
            Console.WriteLine("Ending REST...");
            Console.WriteLine("Ending ExportRequestSegmentedData...");
    

    以及支持方法

        /*** Here are the private functions ***/
        // Encrypting passwords with SHA1 in .NET and Java
        // http://authors.aspalliance.com/thycotic/articles/view.aspx?id=2 
        private static string getBase64Digest(string input)
        {
            SHA1 sha = new SHA1Managed();
            ASCIIEncoding ae = new ASCIIEncoding();
            byte[] data = ae.GetBytes(input);
            byte[] digest = sha.ComputeHash(data);
            return Convert.ToBase64String(digest);
        }
    
        // generate random nonce 
        private static string generateNonce()
        {
            Random random = new Random();
            int len = 24;
            string chars = "0123456789abcdef";
            string nonce = "";
            for (int i = 0; i < len; i++)
            {
                nonce += chars.Substring(Convert.ToInt32(Math.Floor(random.NextDouble() * chars.Length)), 1);
            }
            return nonce;
        }
    
        // Time stamp in UTC string 
        private static string generateTimestamp()
        {
            return DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ");
        }
    
        // C#-Base64 Encoding 
        // http://www.vbforums.com/showthread.php?t=287324 
        public static string base64Encode(string data)
        {
            byte[] encData_byte = new byte[data.Length];
            encData_byte = System.Text.Encoding.UTF8.GetBytes(data);
            string encodedData = Convert.ToBase64String(encData_byte);
            return encodedData;
        }
    

    祝你好运!C