Asp.net mvc 使用MVC4WebAPI从网站获取数据

Asp.net mvc 使用MVC4WebAPI从网站获取数据,asp.net-mvc,asp.net-mvc-4,asp.net-web-api,Asp.net Mvc,Asp.net Mvc 4,Asp.net Web Api,这是这篇文章的后续内容: 下面是我尝试做的一个总结:我想通过MVC4WebAPI与一个网站进行接口。在该站点,用户可以使用用户名和密码登录,然后转到名为“原始数据”的链接以查询该站点的数据 在“原始数据”页面上,有一个“设备”下拉列表、“开始”日期文本框和“结束”日期文本框。给定这三个参数,用户可以单击“获取数据”按钮,并将数据表返回到页面。我要做的是在Azure上托管一个服务,该服务将以编程方式向站点提供这三个参数的值,并将CSV文件从站点返回到Azure存储 托管该站点的公司提供了文档,以编

这是这篇文章的后续内容:

下面是我尝试做的一个总结:我想通过MVC4WebAPI与一个网站进行接口。在该站点,用户可以使用用户名和密码登录,然后转到名为“原始数据”的链接以查询该站点的数据

在“原始数据”页面上,有一个“设备”下拉列表、“开始”日期文本框和“结束”日期文本框。给定这三个参数,用户可以单击“获取数据”按钮,并将数据表返回到页面。我要做的是在Azure上托管一个服务,该服务将以编程方式向站点提供这三个参数的值,并将CSV文件从站点返回到Azure存储

托管该站点的公司提供了文档,以编程方式与该站点交互以检索该原始数据。该文档描述了如何针对其云服务发出请求。必须使用自定义HTTP身份验证方案对请求进行身份验证。以下是身份验证方案的工作原理:

  • 根据用户密码计算MD5哈希
  • 将请求行追加到步骤1中的值的末尾
  • 在步骤2中,将日期标题附加到值的末尾
  • 在步骤3中,将消息正文(如果有)附加到值的末尾
  • 在步骤4的结果值上计算MD5哈希
  • 使用“:”字符作为分隔符,将步骤5中的值附加到用户电子邮件中
  • 根据步骤6中的值计算Base64
  • 我要列出的代码是在Visual Studio 2012、C#、.NET Framework 4.5中完成的。本文中的所有代码都在我的“FileDownloadController.cs”控制器类中。“getMd5Hash”函数接受字符串,并返回MD5哈希:

    //Create MD5 Hash:  Hash an input string and return the hash as a 32 character hexadecimal string. 
        static string getMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
    
            // Convert the input string to a byte array and compute the hash. 
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
    
            // Create a new Stringbuilder to collect the bytes 
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();
    
            // Loop through each byte of the hashed data  
            // and format each one as a hexadecimal string. 
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }
    
            // Return the hexadecimal string. 
            return sBuilder.ToString();
        }
    
    下一个函数创建HTTPClient,生成HTTPRequestMessage,并返回授权。注意:以下是从“原始数据”页面返回数据时从Fiddler返回的URI:GET/rawdata/exportRawDataFromAPI/?devid=3188&fromDate=01-24-2013&toDate=01-25-2013 HTTP/1.1

    首先让我介绍一下这个函数的情况:

  • “WebSiteAuthorization”函数接受“deviceID”、“fromDate”、“toDate”和“密码”
  • 接下来,我声明了三个变量。我不清楚我是否需要一个“消息体”,但我有一个通用版本的设置。其他两个变量保存URI的开头和结尾
  • 我有一个名为“dateHeader”的变量,它保存数据头
  • 接下来,我尝试创建一个HTTPClient,将带有参数的URI分配给它,然后将“application/json”分配为媒体类型。我仍然不太清楚这应该如何构建
  • 在下一步中,根据API文档的要求创建授权,然后返回结果

    public static string WebSiteAuthorization(Int32 deviceid, string fromDate, string toDate, string email, string password)
    {
        var messagebody = "messagebody"; // TODO: ??????????? Message body
        var uriAddress = "GET/rawdata/exportRawDataFromAPI/?devid=";
        var uriAddressSuffix = "HTTP/1.1";
    
        //create a date header
        DateTime dateHeader = DateTime.Today;
        dateHeader.ToUniversalTime();
    
        //create the HttpClient, and its BaseAddress
        HttpClient ServiceHttpClient = new HttpClient();
        ServiceHttpClient.BaseAddress = new Uri(uriAddress + deviceid.ToString() + " fromDate" + fromDate.ToString() + " toDate" + toDate.ToString() + uriAddressSuffix);
        ServiceHttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
        //create the authorization string
        string authorizationString = getMd5Hash(password);
        authorizationString = authorizationString + ServiceHttpClient + dateHeader + messagebody;
        authorizationString = email + getMd5Hash(authorizationString);
        authorizationString = EncodeTo64(authorizationString);
    
        return authorizationString;
    
    }
    
  • 我还没有在Azure上测试过这个。我还没有完成获取文件的代码。我知道我需要做的一件事是确定创建HttpRequestMessage并使用HttpClient发送消息的正确方法。在我所阅读的文档和我所查看的示例中,以下代码片段似乎是实现这一点的可能方法:

    Var serverAddress = http://my.website.com/;
    //Create the http client, and give it the ‘serverAddress’:
    Using(var httpClient = new HttpClient()
    {BaseAddress = new Uri(serverAddress)))
    Var requestMessage = new HttpRequestMessage();
    Var objectcontent = requestMessage.CreateContent(base64Message, MediaTypeHeaderValue.Parse          (“application/json”)
    
    或----

    我不确定这部分代码采用哪种方法


    感谢您的帮助。

    请逐步阅读以了解基本知识。

    谢谢。这很有帮助!
    Var serverAddress = http://my.website.com/;
    //Create the http client, and give it the ‘serverAddress’:
    Using(var httpClient = new HttpClient()
    {BaseAddress = new Uri(serverAddress)))
    Var requestMessage = new HttpRequestMessage();
    Var objectcontent = requestMessage.CreateContent(base64Message, MediaTypeHeaderValue.Parse          (“application/json”)
    
    var formatters = new MediaTypeFormatter[] { new jsonMediaTypeFormatter() }; 
    HttpRequestMessage<string> request = new HttpRequestMessage<string>
    ("something", HttpMethod.Post, new Uri("http://my.website.com/"), formatters); 
    
    request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
    
    var httpClient = new HttpClient(); 
    var response = httpClient.SendAsync(request);
    
    Client = new HttpClient();
    var request = new HttpRequestMessage
                     {
                         RequestUri = "http://my.website.com/",
                         Method = HttpMethod.Post,
                         Content = new StringContent("ur message")
                     };