我做了一个小服务,应该向第三方资源发送请求,然后我接受来自该资源的 json。在 ASP.NET С# 上实现
错误是我通过邮递员发送post请求,然后下载很长,之后就出来了:“Could not get response” “Error: read ECONNRESET” 应用程序正在运行。在 http://localhost:"port 发布 asp.net" 上启动 如何正确发送请求?如果您发现代码中有任何问题,请告诉我,以便我理解和修复。
代码:
请求:
{
"str":"d02d355e,b4ad,7120,8e78,4a0904b37179",
"token":"i116ie9qn7vpa0k"
}
服务:
public class ServiceFromGetStatus
{
private IHttpClientFactory _clientFactory;
Attributes attributes;
string errorMsg;
public ServiceFromGetStatus( IHttpClientFactory httpClient)
{
_clientFactory = httpClient;
}
//oneMethod
public static string[] ShowAllOrder(string liststring)
{
string[] masString = liststring.Split("[ ,]+");
return masString;
}
//twoMethod
public async Task<List<ShowModelAttributes>> sendRestArrayOrderId(StrAndToken str)
{
string[] mas = ShowAllOrder(str.str);
var restLst = new List<Attributes>();
foreach (string i in mas)
{
var urlFormat = string.Format("https://example.ru/rest/getStatus.do?token={0}&orderId={0}/", str.token, i);
var paramsDto = new HttpRequestMessage(HttpMethod.Get, urlFormat);
var client = _clientFactory.CreateClient();
HttpResponseMessage response = await client.SendAsync(paramsDto);
attributes = await response.Content.ReadFromJsonAsync<Attributes>();
restLst.Add(attributes);
}
return toShowModel(restLst);
}
//threeMethod
public List<ShowModelAttributes> toShowModel(List<Attributes> listDto)
{
var bb = new List<ShowModelAttributes>();
foreach (Attributes i in listDto)
{
if (i.orderStatus == 2)
{
bb.Add(new ShowModelAttributes(i.orderNumber,
i.orderStatus,
i.amount,
i.Info.depositedAmount,
i.depositedDate,
i.cardAuthInfo.Pan,
i.terminal,
i.authRefNum,
i.cardAuthInfo.Code
));
}
else if (i.orderStatus == 1)
{
bb.Add(new ShowModelAttributes(i.orderNumber,
i.orderStatus,
i.amount,
i.Info.depositedAmount,
i.depositedDate,
i.cardAuthInfo.Pan,
i.terminal,
i.authRefNum,
i.cardAuthInfo.approvalCode
));
}
}
return bb;
}
型号:
public class Attributes
{
public string errorCode { get; set; }
public string errorMessage { get; set; }
public string orderNumber { get; set; }
public int orderStatus { get; set; }
public int actionCode { get; set; }
public string actionCodeDescription { get; set; }
public int amount { get; set; }
public string currency { get; set; }
public long date { get; set; }
public long depositedDate { get; set; }
public string orderDescription { get; set; }
public string ip { get; set; }
public List<object> Params { get; set; }
public List<object> trans { get; set; }
public List<Attributes> attributes { get; set; }
public CardAuthInfo cardAuthInfo { get; set; }
public long authDateTime { get; set; }
public string terminal { get; set; }
public string authRefNum { get; set; }
public Info info { get; set; }
public Inter inter { get; set; }
}
public class CardAuthInfo
{
public string Pan { get; set; }
public string expiration { get; set; }
public string Name { get; set; }
public string Code { get; set; }
public string pan { get; set; }
}
public class Info
{
public string paymentState { get; set; }
public int approvedAmount { get; set; }
public int depositedAmount { get; set; }
public int refundedAmount { get; set; }
}
public class Inter
{
public string Name { get; set; }
public string CountryCode { get; set; }
public string CountryName { get; set; }
}
public class Root
{
public string name { get; set; }
public string value { get; set; }
}
SturtUp:
public class Startup
{
public Startup(IConfiguration configuration)
{
Сonfiguration = configuration;
}
public IConfiguration Сonfiguration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<DataBaseContex>(o => o.UseNpgsql(Сonfiguration.GetConnectionString("DefaultConnection")
));
// services.AddScoped<DataBaseContex>(p => p.GetService<DataBaseContex>());
services.AddControllers();
services.AddScoped<ServiceFromGetStatus>();
services.AddHttpClient<ServiceFromGetStatus>();
services.AddScoped<Attributes>();
services.AddScoped<ShowModelAttributes>();
services.AddScoped<StrAndToken>();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
控制器:
[Route("api/command")]
[ApiController]
public class ValuesController : ControllerBase
{
ServiceFromGetStatus serviceFromGetStatus;
public ValuesController(ServiceFromGetStatus serviceFromGetStatus)
{
this.serviceFromGetStatus = serviceFromGetStatus;
}
[HttpPost]
public async Task<List<ShowModelAttributes>> showModels([FromBody]StrAndToken str)
{
return await serviceFromGetStatus.sendRestArrayOrderId(str);
}
}