the first request i=1 is done but the next is getting this message:
{"the request was aborted: the connection was closed unexpectedly."}.
Here is my code
for (var i = 1; i <= 4; i++)
{
var token = GetToken();
string token_cookie = ConfigurationManager.AppSettings["token_cookie"].ToString();
var request = System.Net.WebRequest.Create(url + "/Revenue/GetList") as System.Net.HttpWebRequest;
request.Method = "POST";
request.UserAgent = "api-explorer-client";
request.ContentType = "application/json";
request.Headers["Cookie"] = token_cookie;
request.Headers["Authorization"] = "Bearer " + token;
string body = "{" + "\n " + "\"DateFrom\": \"" + "2024-09-01" + "\","
+ "\n " + "\"DateTo\": \"" + DateTime.Now.ToString("yyyy-MM-dd") + "\","
+ "\n " + "\"BranchID\": \"" + "0" + "\","
+ "\n " + "\"DataType\": \"" + "all" + "\","
+ "\n " + "\"PagingNumber\": \"" + 1.ToString() + "" + "\""
+ "\n " + "}";
byte[] postBytes = Encoding.UTF8.GetBytes(body);
//request.ContentLength = postBytes.Length;
using (Stream stream = request.GetRequestStream())
{
stream.Write(postBytes, 0, postBytes.Length);//error here
}
string responseContent = "";
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
responseContent = streamReader.ReadToEnd();
RevenueList revenueList = JsonConvert.DeserializeObject<RevenueList>(responseContent);
DataTable RevList = new DataTable();
RevList = ToDataTable<RevenueListData>(revenueList.Data);
InsertBulkTable(RevList, "RevenueList");
//currentPage++;
streamReader.Close();
}
}
request.Abort();
}
}
i have also use another way but still got the message
I advise you to use an HTTPClient, I made a version of your code that could work with it.
If you are not familliar with How to Post trough HTTPClient go read this
public class SendFourRequest()
{
private readonly HttpClient _client;
private string address = "https://localhost:3333/api" // Choose the target address of your client
public SendFourRequest(HttpClient httpClient)
{
_client = httpClient;
}
public Task sendRequestsAsync()
{
for (int i = 0; i < 4; i++)
{
// This create a model with all the propertiew wou wanted
var bodyContent = new RequestBodyModel()
{
DateFrom = DateTime.Parse("2024-09-01"),
DateTo = DateTime.Now(),
PagingNumber = i
};
// creation of the request
var request = new HttpRequestMessage(HttpMethod.Post, address);
// Adding the body as a jsonContent
request.Content = JsonContent.Create(bodyContent);
var response = await _client.SendAsync(request);
// Here you can check the response if wanted
}
}
private class RequestBodyModel()
{
public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; }
public int BranchID { get; set; } = 0
public string DataType { get; set; } = "all"
public int PagingNumber { get; set; }
}
}
The HTTPClient is in my case a Parameter but you can create one in the class. If you need to add cookie to it, go look how to set Cookie on HTTPClient