An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
HttpClient client - empty token
Markus Freitag
3,791
Reputation points
Hello,
I have received the following installation from the customer. How can I establish a connection? How can I connect with an empty token? Do you know a simple test tool where I can create a test server without lengthy training. As a second variant with authorization and password. How to do it right?
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
private void btnWorkorder_Click(object sender, EventArgs e)
{
HttpResponseMessage response;
string myUrl = "/test/OrderSystem/White";
try
{
RequestWorkorder p = new RequestWorkorder()
{
Program = txtProgramName.Text,
Workorder = txtWorkOrder.Text
};
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(BaseAddress);
client.Timeout = new TimeSpan(0, 0, 25);
string authorization = $"{""}:{""}";
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", ""); // or BEARER?
urlParameters = $@"{myUrl}";
string request = JsonConvert.SerializeObject(p, Formatting.Indented);
response = client.PostAsJsonAsync(myUrl, p).Result;
}
if (response != null)
{
Log.Info($" Response -> IsSuccessStatusCode= '{response.IsSuccessStatusCode}'");
}
else
{
throw new Exception($" response= 'null', please check the system");
}
var dataObjects = response.Content.ReadAsStringAsync();
ResponseStandard resBase = JsonConvert.DeserializeObject<ResponseStandard>(dataObjects.Result);
if (resBase.Status == 1) // all fine!
{
}
else
{
throw new Exception($"Status code= '{resBase.Status}' is not 1! Please check the system {resBase.Msg}");
}
MessageBox.Show($"StateCode= '{resBase.Status}'\nError= '{resBase.Error}' \n\nDone successful!", "Info...", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (WebException ex)
{
}
catch (Exception exNormal)
{
}
}
Developer technologies | C#
Developer technologies | C#
Sign in to answer