Кажется, вы не знаете, как сделать запрос POST
. Вы можете попробовать ниже фрагмент.
ApiRequestModel _requestModel = new ApiRequestModel();
_requestModel.RequestParam1 = "YourValue";
_requestModel.RequestParam2= "YourValue";
var jsonContent = JsonConvert.SerializeObject(_requestModel);
var authKey = "c4b3d4a2-ba24-46f5-9ad7-ccb4e7980da6";
var requestURI = "http://10.10.102.109/api/v1/routing/windows/Window1";
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(requestURI);
request.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
request.Headers.Add("Authorization", "Basic" + authKey);
var response = await client.SendAsync(request);
//Check status code and retrive response
if (response.IsSuccessStatusCode)
{
dynamic objResponse = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync());
}
else
{
dynamic result_string = await response.Content.ReadAsStringAsync();
}
}
Обновление: Согласно вашему скриншоту в комментарии, вы также можете попробовать этот способ.
private async Task<string> HttpRequest()
{
//Your Request End Point
string requestUrl = $"http://10.10.102.109/api/v1/routing/windows/Window1";
var requestContent = new HttpRequestMessage(HttpMethod.Post, requestUrl);
//Request Body in Key Value Pair
requestContent.Content = new FormUrlEncodedContent(new Dictionary<string, string>
{
["CanYCentre"] = "540",
["CanXCentre"] = "960",
});
requestContent.Headers.Add("Authorization", "Basic" + "YourAuthKey");
HttpClient client = new HttpClient();
//Sending request to endpoint
var tokenResponse = await client.SendAsync(requestContent);
//Receiving Response
dynamic json = await tokenResponse.Content.ReadAsStringAsync();
dynamic response = JsonConvert.DeserializeObject<dynamic>(json);
return response;
}
Обновление 2:
If you still don't understand or don't know how you would implement it Just copy and paste below code snippet.
private static async Task<string> HttpRequest()
{
object[] body = new object[]
{
new { CanYCentre = "540" },
new { CanYCentre = "960" }
};
var requestBody = JsonConvert.SerializeObject(body);
using (var client = new HttpClient())
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri("http://10.10.102.109/api/v1/routing/windows/Window1");
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("Authorization", "Basic" + "YourAuthKey");
var response = await client.SendAsync(request);
var responseBody = await response.Content.ReadAsStringAsync();
dynamic result = JsonConvert.DeserializeObject<dynamic>(responseBody);
return result;
}
}
Надеюсь, это поможет вам. Если у вас возникнут какие-либо проблемы, не стесняйтесь поделиться.