3
我收到C#Client-Service中的代碼並使用HTTP協議。 我需要從我在網上找到(不知道我所做的是正確的),我擴大已經存在的BaseHttpActionResult
類中添加一個新的動作誰返回Datetime
類型,所以:如何讀取包含日期時間類型的BaseHttpActionResult的響應
public BaseHttpActionResult(HttpStatusCode code, DateTime content)
{
_code = code;
_content = content;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = new HttpResponseMessage
{
StatusCode = _code,
Content = new ObjectContent(typeof(DateTime), _content, new JsonMediaTypeFormatter())
};
return Task.FromResult(response);
}
服務添加此動作:
[Route("StatusUpdate")]
[HttpGet]
public IHttpActionResult StatusUpdate()
{
return new BaseHttpActionResult(HttpStatusCode.OK, DateTime.Today);
}
和客戶端的請求:
public class HttpRequestsHandler
{
public static DateTime HttpGet(string requestUrl)
{
using (var handler = new WebRequestHandler())
{
handler.ServerCertificateValidationCallback = (sender, certificate, chain, errors) => true;
using (var httpClient = new HttpClient(handler)
{
BaseAddress = new Uri(requestUrl),
Timeout = Timeout.InfiniteTimeSpan
})
{
using (var response = httpClient.GetAsync(requestUrl).Result)
{
if (response.StatusCode != HttpStatusCode.OK)
{
var errorMessage = string.Format("HttpRequestTask: Request failed with status {0} for URL: {1}", response.StatusCode, requestUrl);
throw new HttpException(errorMessage, (int)response.StatusCode);
}
response.???????
}
}
}
}
}
事情是,我怎麼看Datetime
值從響應? 我在網上看到,有方法命名爲內容和標題和一些例子,但我不明白如何使用它...
希望任何輸入。
正是我需要的,非常感激! –