我正在尝试使用以下代码从移动应用程序向我的网站接收JSON请求:
[HttpPost]
[Route("{platform:minlength(2)}/{leagueID:int}/leagueteams")]
public IActionResult ExportLeagueTeams([FromRoute] string platform,
[FromRoute] int leagueID)
{
if (!string.IsNullOrEmpty(this.logFile))
{
try
{
using (StreamWriter writer = new StreamWriter(this.logFile, true))
{
writer.WriteLineAsync("***").Wait();
writer.WriteLineAsync("Testing this log").Wait();
writer.WriteLineAsync("Platform: " + platform).Wait();
writer.WriteLineAsync("LeagueID: " + leagueID).Wait();
writer.WriteLineAsync("HEADERS:").Wait();
// Get the headers
foreach (var header in Request.Headers)
{
writer.WriteLineAsync(header.Key + ": " + header.Value).Wait();
}
writer.WriteLineAsync("BODY (raw):").Wait();
// get the Body of the request
using (var reader = new StreamReader(Request.Body))
{
var body = reader.ReadToEnd();
writer.WriteLineAsync(body).Wait();
}
}
return Ok();
}
catch (Exception e)
{
return BadRequest();
}
}
else
{
return BadRequest();
}
}
这将从我尝试使用的移动应用程序获取请求,但是,请求正文看起来很奇怪。它肯定是加密的或二进制的。我无法从日志中复制和粘贴,因为正文仅被复制并粘贴为
身体:
请求标头如下:
HEADERS:
Connection: Keep-Alive
Content-Type: application/json
Content-Encoding: gzip
Accept: application/json
Host: mywebsite.azurewebsites.net
Max-Forwards: 10
User-Agent: ProtoHttp 1.3/DS 15.1.2.2.0 (Android)
Content-Length: 1811
X-WAWS-Unencoded-URL: /api/Madden/ps4/6909313/leagueteams
CLIENT-IP: 73.13.26.24:47529
X-ARR-LOG-ID: 425fb24e-aa9f-4422-9dd2-b3b407240453
DISGUISED-HOST: mywebsite.azurewebsites.net
X-SITE-DEPLOYMENT-ID: mysebsite
WAS-DEFAULT-HOSTNAME: mywebsite.azurewebsites.net
X-Original-URL: /api/Madden/ps4/6909313/leagueteams
X-Forwarded-For: 73.13.26.24:47529
X-ARR-SSL: 2048|256|C=US, S=Washington, L=Redmond, O=Microsoft Corporation, OU=Microsoft IT, CN=Microsoft IT TLS CA 4|CN=*.azurewebsites.net
X-Forwarded-Proto: https
MS-ASPNETCORE-TOKEN: 8c86c695-eec2-4328-a7ed-b2c2f10b9603
X-Original-For: 127.0.0.1:56457
X-Original-Proto: http
如何解码/解密请求正文?
答案 0 :(得分:1)
我能够做到!文件即将压缩。
使用Anemonis.AspNetCore.RequestDecompression,我可以在Startup.cs
代码中添加以下内容
ConfigureServices
代码
/// <summary>
/// This method gets called by the runtime. Use this method to add services to the container.
/// </summary>
/// <param name="services">Services to include</param>
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
// Add Decompression options
var decompressOptions = new RequestDecompressionOptions();
decompressOptions.UseDefaults();
decompressOptions.AddProvider<GzipDecompressionProvider>();
decompressOptions.SkipUnsupportedEncodings = false;
services.AddRequestDecompression(decompressOptions);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
var connection = Configuration.GetConnectionString("MySiteUsers");
var mailingListConnection = Configuration.GetConnectionString("MailingList");
services.AddDbContext<MySiteContext>(options => options.UseSqlServer(connection));
services.AddDbContext<MySiteMailingListContext>(options => options.UseSqlServer(mailingListConnection));
}
Configure
代码
/// <summary>
/// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
// This is where you use the Request Decompression
app.UseRequestDecompression();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
});
}
非常感谢@IanKemp向正确的方向推进