Asp.net mvc 使用MVC4WebAPI从网站获取数据
这是这篇文章的后续内容: 下面是我尝试做的一个总结:我想通过MVC4WebAPI与一个网站进行接口。在该站点,用户可以使用用户名和密码登录,然后转到名为“原始数据”的链接以查询该站点的数据 在“原始数据”页面上,有一个“设备”下拉列表、“开始”日期文本框和“结束”日期文本框。给定这三个参数,用户可以单击“获取数据”按钮,并将数据表返回到页面。我要做的是在Azure上托管一个服务,该服务将以编程方式向站点提供这三个参数的值,并将CSV文件从站点返回到Azure存储 托管该站点的公司提供了文档,以编程方式与该站点交互以检索该原始数据。该文档描述了如何针对其云服务发出请求。必须使用自定义HTTP身份验证方案对请求进行身份验证。以下是身份验证方案的工作原理: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存储 托管该站点的公司提供了文档,以编
//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
首先让我介绍一下这个函数的情况:
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;
}
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")
};