我们正在将我们的应用程序从.net框架升级到.net core 2.0.
在其中,我们使用a HttpWebRequest来联系AllowAutoRedirect设置为false 的站点.当代码执行时request.GetResponse()该站点将返回302响应,在.net框架中是可以的 - 您可以接受响应并处理它(我们在set-cookie标头值之后).
但是,在.net core 2.0中,抛出WebException:
The remote server returned an error: (302) Found.
Run Code Online (Sandbox Code Playgroud)
我的理解是不正确的,因为302应该导致抛出异常,而如果AllowAutoRedirect设置为false,那么仍然应该返回响应?有没有办法触发.net框架中遇到的相同行为?
c# asp.net system.net.httpwebrequest asp.net-core asp.net-core-2.0
我们的控制台应用程序每分钟都会向Facebook发送数百个WebRequests(使用多个应用程序和数百个访问令牌).现在,他们开始失败并在标题中显示异常消息("请求已中止:请求已取消").我们在互联网上搜索了几个小时,并尝试了所有可能的解决方案,但没有任何帮助.
这些没有帮助:
webRequest.Timeout = 20000; //A request that didn't get respond within 20 seconds is unacceptable, and we would rather just retry.
webRequest.KeepAlive = false;
webRequest.ProtocolVersion = HttpVersion.Version10;
webRequest.ServicePoint.Expect100Continue = false;
Run Code Online (Sandbox Code Playgroud)
任何人有任何其他想法?
编辑:
异常的ToString:System.Net.WebException:请求已中止:请求已取消.---> System.Net.WebException:请求在System.Net.HttpWebRequest.FindServicePoint(布尔forceFind)的System.Net.ServicePointManager.FindServicePoint(Uri地址,IWebProxy代理,ProxyChain&chain,HttpAbortDelegate&abortDelegate,Int32&abortState)中被取消.在System.Net.HttpWeb上的System.Net.HttpWebRequest.DoSubmitRequestProcessing(异常和异常)处(异常E)---内部异常堆栈跟踪结束---在WebException消息的System.Net.HttpWebRequest.GetResponse()处:请求已中止:请求已取消.
edit2:我们没有达到极限.我们知道什么时候发生,问题不是那样.我们已经这样做了两年,这件事在整个过程中只发生过两次.根据AccessToken,我们每分钟只做2-3次请求,Facebook上的限制是600个请求/ accesstoken/ip.
edit3:我想为有这个或类似问题的人添加一个额外的提示:确保你处理你的RequestStream,你的Response和你的ResponseStream对象.
c# http-error system.net.webexception system.net.httpwebrequest
我正在编写一个Web API项目(MVC架构).我有一个实用程序类,其方法返回一个HttpResponseMessage.如果此类放在Utility文件夹中,一切正常.如果我尝试将该类放在App_Code文件夹中,我会收到消息"找不到类型或命名空间名称'HttpResponseMessage'."
下面提供了发生这种情况的方法的示例.需要做什么才能在App_Code文件夹中将变量或方法声明为HttpResponseMessage?
public HttpResponseMessage GetResponseMessage<T>(T item, MediaTypeFormatter mtFormatter)
{
HttpResponseMessage response = new HttpResponseMessage()
{
Content = new ObjectContent<T>(item, mtFormatter)
};
return response;
}
Run Code Online (Sandbox Code Playgroud) c#-4.0 asp.net-web-api system.net.httpwebrequest visual-studio-2012
我有一个场景,我需要调用我的Web API Delete方法,如下所示:
// DELETE: api/products/{id}/headers
[HttpDelete("{id}/headers")]
public void DeleteProductHeaders(int id, [FromBody] string query)
{
}
Run Code Online (Sandbox Code Playgroud)
诀窍是,为了获得查询,我需要通过正文发送它,而DeleteAsync没有像post那样的json参数.有谁知道如何使用c#中的System.Net.Http客户端来做到这一点?
// Delete a product's headers
public void DeleteProductHeaders(int id, string query)
{
using (var client = GetClient())
{
HttpResponseMessage response;
try
{
// HTTP DELETE
response = client.DeleteAsync($"api/products/{id}/headers").Result;
}
catch (Exception ex)
{
throw new Exception("Unable to connect to the server", ex);
}
}
return retVal;
}
Run Code Online (Sandbox Code Playgroud) 我正在使用WebRequestHandler在我的完整堆栈.NET应用程序中设置CachePolicy和AuthenticationLevel.现在我将我的应用程序迁移到.NET核心,无法找到这些属性或WebRequestHandler的替代方法.有帮助吗?以下是我的用法:
var httpClientHandler = new WebRequestHandler
{
UseProxy = true,
UseCookies = false,
CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore),
AuthenticationLevel = AuthenticationLevel.MutualAuthRequired
};
Run Code Online (Sandbox Code Playgroud) 我已经下载了最新的.NET Framework,我正在使用VS 2017 15.8.7上的.NET Core 2.0应用程序.这是我安装的软件包.
using (var client = new PowerBIClient(new Uri(ApiUrl), tokenCredentials))
{
}
Run Code Online (Sandbox Code Playgroud)
我在这一行收到错误,说:
FileNotFoundException: Could not load file or assembly 'System.Net.Http.WebRequest, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.
Run Code Online (Sandbox Code Playgroud)
这是我的.csproj
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.PowerBI.Api" Version="2.0.14" />
<PackageReference Include="Microsoft.PowerBI.Core" Version="1.1.11" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.6" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误.我可以添加一个引用来使它工作吗?
[更新] 我在我的csproj中添加了以下行,我不再收到此错误.
<ItemGroup>
<Reference Include="System.Net.Http">
<HintPath>..\..\..\..\..\..\Windows\Microsoft.NET\Framework\v4.0.30319\System.Net.Http.dll</HintPath>
</Reference>
<Reference Include="System.Net.Http.WebRequest">
<HintPath>..\..\..\..\..\..\Windows\Microsoft.NET\Framework\v4.0.30319\System.Net.Http.WebRequest.dll</HintPath>
</Reference>
</ItemGroup>
Run Code Online (Sandbox Code Playgroud) asp.net system.net system.net.httpwebrequest asp.net-core-2.0
我发现当我使用HttpWebRequest建立SSL\TLS连接时,调用时需要近30秒
request.GetRequestStream()
Run Code Online (Sandbox Code Playgroud)
当我启用了跟踪启用堆栈跟踪时,我发现2s会找到poxy,所以我在app.config中禁用了它:
<system.net>
<defaultProxy enabled="false" useDefaultCredentials="false">
<proxy/>
<bypasslist/>
<module/>
</defaultProxy>
</system.net>
Run Code Online (Sandbox Code Playgroud)
下一个接近28s的点是在
at System.Net.Security.SecureChannel.AcquireClientCredentials(Byte[]& thumbPrint)
Run Code Online (Sandbox Code Playgroud)
在检查方法体后,我发现了对X509Chain.Build()的调用,并且花了将近25秒来构建证书链.
有趣的是,当你构造新的HttpWebReqest并再次尝试(没有app重启)时,执行请求需要几个ms.
谁能建议做什么?缓存请求不是一个选项,它应该从app运行快速.
更新:
我在X509Chain.BuildChain()中发现需要30秒的调用,它是:
if (!CAPISafe.CertGetCertificateChain(hChainEngine, pCertContext, ref pTime, invalidHandle, ref cert_chain_para, dwFlags, IntPtr.Zero, ref ppChainContext))
Run Code Online (Sandbox Code Playgroud)
在CAPISafe中声明的方法为:
[DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool CertGetCertificateChain([In] IntPtr hChainEngine, [In] SafeCertContextHandle pCertContext, [In] ref System.Runtime.InteropServices.ComTypes.FILETIME pTime, [In] SafeCertStoreHandle hAdditionalStore, [In] ref CAPIBase.CERT_CHAIN_PARA pChainPara, [In] uint dwFlags, [In] IntPtr pvReserved, [In, Out] ref SafeCertChainHandle ppChainContext);
Run Code Online (Sandbox Code Playgroud)
所以,它是加密API函数CertGetCertificateChain 还是不知道,下一步做什么...
更新:
我试图禁用CRL和OCSP检查,仍然没有效果:
添加到App.config …
当我为WebApi安装NuGet包时,它给了我1.0.0.0版的System.Net.Http,但它引用了版本2.
我似乎无法找到我的生活版本2.当我下载.net 4.5时,我得到System.Net.Http版本4.0.315.x,但我不能使用它,因为Web Api程序集引用版本2.0.0.0.
有谁知道我在哪里可以找到这个版本2.0.0.0?我已经尝试过NuGet包管理器,mvc4,.net 4.5,没有什么能给我版本2.
提前致谢.
system.net .net-assembly nuget-package asp.net-web-api system.net.httpwebrequest
我一直在做这方面的研究,我找不到直接的答案.
是否有一些存储在容器中的cookie,我无法使用Response.Cookies集合?如何在使用这些对象的请求之间处理cookie?有些饼干存放在容器中但不存储在其他容器中吗?
asp.net asp.net-mvc httpwebrequest system.net.httpwebrequest
背景:我已在Google Developer Console上为我的项目设置了服务帐户,并使用服务帐户电子邮件,证书和密码,并按照GoogleAPisSample Plus.ServiceAccount中提供的示例提供.下面的代码片段是我的Windows服务应用程序的一部分:
var List<string> Scopes = new List<string> { "https://www.googleapis.com/auth/analytics.readonly" };
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(ServiceAccountEmail)
{
Scopes = Scopes
}.FromCertificate(certificate));
if(credential.RequestAccessTokenAsync(CancellationToken.None).Result)
{
AuthenticationKey = credential.Token.AccessToken;
}
Run Code Online (Sandbox Code Playgroud)
当我在我的本地开发机器上安装并运行此服务时,它会完成credential.RequestAccessTokenAsync并接收AccessToken并继续执行该服务并正确读取Analytics数据.
但是,当它部署在我们的QA环境(Window Server 2008 R2 Standard)上并再次运行时,在调用credential.RequestAccessTokenAsync时会抛出以下异常:
System.AggregateException: One or more errors occurred. ---> System.MissingMethodException: Method not found: 'System.Net.HttpStatusCode System.Net.Http.HttpResponseMessage.get_StatusCode()'.
at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
at OurApplication.SchedulerService.GoogleAnalytics.OAuth2.ServiceAccountCredential.<RequestAccessTokenAsync>d__b.MoveNext()
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task`1.get_Result()
at OurApplication.SchedulerService.GoogleAnalytics.GADataFetcher.AuthenticateAndAuthorize()
at OurApplication.SchedulerService.GoogleAnalytics.GADataFetcher..ctor()
at OurApplication.SchedulerService.GoogleAnalytics.GoogleAnalyticsService.GoogleAnalyticsTopPerformances(Int32 sessID, String sessToken)
---> (Inner Exception …Run Code Online (Sandbox Code Playgroud) c# ×4
asp.net ×3
.net ×2
system.net ×2
.net-core ×1
asp.net-core ×1
asp.net-mvc ×1
c#-4.0 ×1
http ×1
http-error ×1
ssl ×1