Lat*_*nis 2 c# asp.net http-status-codes asp.net-core-mvc asp.net-core
我正在运行一个安全和不安全域(包括http://和https://)的列表,并希望返回其状态代码.这怎么可能在ASP.Net Core 1.0中?
到目前为止,我有
foreach(var item in _context.URLs.ToList())
{
// Do something here with item.Domain to check for status
// For example, if item.Domain was https://example.com..
}
Run Code Online (Sandbox Code Playgroud)
我用这种方法用常规ASP.Net语法尝试了它:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(item.Domain);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Run Code Online (Sandbox Code Playgroud)
问题是,GetResponse在ASP.Net Core中不起作用
任何人都可以帮我解决一个有效的解决方案,以便返回的变量是状态吗?
例如:200,500,404..
编辑 - 这是我的完整控制器和解决方案:
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using MyApp.Models;
using System.Threading.Tasks;
using System.Net;
using System.Net.Http;
namespace MyApp.Controllers.Api
{
public class URLsController : Controller
{
private MyAppDBContext _context;
public class newLink
{
public string Domain { get; set; }
public HttpStatusCode Status { get; set; }
}
public async Task<HttpStatusCode> GetStatusCodes(string url)
{
var client = new HttpClient();
var response = await client.GetAsync(url);
return response.StatusCode;
}
public URLsController(MyAppDBContext context)
{
_context = context;
}
[HttpPost("api/URLs")]
public async Task<IActionResult> Post(string url)
{
if (url != "")
{
// I pass a URL in through this API and add it to _context.URLs..
// I execute a status code check on the URLs after this if statement
}
List<newLink> list = new List<newLink> ();
foreach (var item in _context.URLs.ToList())
{
newLink t = new newLink();
t.Domain = item.Domain;
t.Status = await GetStatusCodes(item.Domain);
list.Add(t);
}
return Ok(list);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这将以此格式返回一个数组:
[{"域名":" https://example1.com/ ","状态":200},
{"域名":" https://example2.com/ ","状态":200},
{"域名":" https://example3.com/ ","状态":200}]
您可以使用,HttpClient因为它更容易使用(您不需要捕获非成功状态代码的WebExceptions,就像您必须使用plain一样HttpWebRequest,然后从异常中提取HTTP状态代码).
您可以编写一个帮助方法,给定一个URL列表将返回相应状态代码的列表.这将使您的代码更加分离.不要违反单一责任原则.一种方法,应该不会做超过1米具体的东西(在你的榜样,你在混合一些DB调用和HTTP调用到这是不好的做法,一个单一的方法).
public async Task<IList<HttpStatusCode>> GetStatusCodes(IList<string> urls)
{
var client = new HttpClient();
var result = new List<HttpStatusCode>();
foreach (var url in urls)
{
var response = await client.GetAsync(url);
result.Add(response.StatusCode);
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
备注1:如果您尝试呼叫的URL不是DNS可解析的,或者您的呼叫应用程序无法访问目标端口上的指定地址,则由于显而易见的原因,您将无法获得任何状态代码.你会得到一个很好的例外.考虑处理这种情况.在这种情况下,您可以决定在结果集合中返回什么.
备注2:为了确定HTTP状态代码而发出GET请求可能是一种浪费,因为您丢弃了已经通过线路传输的响应主体.如果远程资源响应HEAD请求,则可能是确定服务器是否处于活动状态的更有效方法.但请谨慎考虑,因为它取决于您调用的Web端点的具体情况.
备注3:你无疑注意到这种方法是async.好吧,如果你打算在.NET Core下开发,你最好习惯它.您当然可以通过阻止调用线程来违反框架为您提供的内置异步模式:
var urls = _context.URLs.ToList();
IList<HttpStatusCode> statusCodes = GetStatusCodes(urls).GetAwaiter().GetResult();
Run Code Online (Sandbox Code Playgroud)
但这是一种非常糟糕的做法.更惯用的工作方式是使所有方法在整个链中异步,直到到达主调用方法,该方法通常由框架本身提供,并且也可以是异步的.例如,如果您在Web API操作中调用它,则只需将其设置为异步:
[HttpGet]
[Route("api/foos")]
public async Task<IActionResult> Get()
{
var urls = _context.URLs.ToList();
IList<HttpStatusCode> statusCodes = await GetStatusCodes(urls);
return this.Ok(statusCodes);
}
Run Code Online (Sandbox Code Playgroud)