我目前正在尝试使用.NET Core和MediaTypeFormatters中的HttpClient进行一些JSON格式化.特别是函数"ReadAsAsync(...,MediaTypeFormatter,...)"(https://msdn.microsoft.com/de-de/library/system.net.http.httpcontentextensions.readasasync(v=vs.118) .aspx)在HttpContent-Class的.NET Framework中可用非常有用.据我所知,它可以在NuGet包中找到Microsoft.AspNet.WebApi.Client但我无法下载它,因为它在.NET Core中不受支持.
虽然我已经读过,但它应该是:
我知道可以使用Newtonsoft等进行格式化.
但有人知道,如果那个软件包有一天会再次在.NET Core中提供吗?我真的找不到任何信息......
谢谢
我在Android中遇到HttpClient问题:通过使用以下代码,我想通过webview登录来使用之前已设置的cookie.所以登录数据应该在那里,确实存在,我测试了它.但是当我在httppost或httpget中使用cookie时,它不使用登录数据.但这些cookie实际上应该足以接收需要登录的页面,不应该这样吗?我不确定是否需要以特殊方式将cookie发送到服务器,或者是否足以将其加载到httpcontext中.这是代码:
DefaultHttpClient httpclient = new DefaultHttpClient();
CookieStore lCS = new BasicCookieStore();
if (CookieManager.getInstance().getCookie(pUrl) != null) {
String cookieString = CookieManager.getInstance().getCookie(pUrl);
String[] urlCookieArray = cookieString.split(";");
for (int i = 0; i < urlCookieArray.length; i++) {
System.out.println(urlCookieArray[i]);
String[] singleCookie = urlCookieArray[i].split("=");
Cookie urlCookie = new BasicClientCookie(singleCookie[0], singleCookie[1]);
lCS.addCookie(urlCookie);
}
}
HttpContext localContext = new BasicHttpContext();
httpclient.setCookieStore(lCS);
localContext.setAttribute(ClientContext.COOKIE_STORE, lCS);
HttpPost httppost = new HttpPost(pUrl);
// get the url connection
try {
StringBuilder sb = new StringBuilder();
HttpResponse response = httpclient.execute(httppost, localContext);
InputStream is …Run Code Online (Sandbox Code Playgroud) 我正在尝试为IWebHostBuilder执行一些代码的新扩展方法,例如在某处注册。重要的是,它在应用程序关闭时也会注销。
我添加了一些代码来显示我想做什么。这里的问题是,的实例IApplicationLifetime尚不可用。这种新的扩展方法是在构建WebHost的管道中最后添加的。
public static IWebHostBuilder UseHttpGatewayappRegistration(this IWebHostBuilder webHostBuilder)
{
webHostBuilder.ConfigureServices(services =>
{
var sp = services.BuildServiceProvider();
// This instance is not available
IApplicationLifetime applicationLifetime = sp.GetService<IApplicationLifetime>();
// These instances are ok
AppSettings appSettings = sp.GetService<AppSettings>();
ILoggerFactory loggerFactory = sp.GetService<ILoggerFactory>();
var registration = new Registration(loggerFactory.CreateLogger<Registration>());
applicationLifetime.ApplicationStopping.Register(() =>
{
registration.Unregister();
});
});
return webHostBuilder;
}
Run Code Online (Sandbox Code Playgroud)
IApplicationLifetime即使在构建WebHost管道的管道中最后添加了此扩展方法,实例为何也为null?这将是巨大的,如果有人向我提供有关的所有“ConfigureServices”方法的执行顺序和如何或者如果它是在所有可能使用一些信息IApplicationLifetime的ConfigureServices方法。
我知道无需使用即可完成所有操作WebHostBuilder,但在我看来这样做很合乎逻辑,而且我也认为必须有一种方法。
不幸的是,我在网上找不到太多信息...
谢谢。
编辑:我找到了一种在ConfigureServices方法中使用DI的方法。我相应地编辑了问题。这对我有帮助:如何在ASP.NET Core中的ConfigureServices中解析实例
.net-core ×2
c# ×2
.net ×1
android ×1
asp.net-core ×1
cookies ×1
core ×1
httpclient ×1
java ×1