Share via

HttpClient client - empty token

Markus Freitag 3,791 Reputation points
2023-08-30T17:12:03.9533333+00:00

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#

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.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.