[ACCEPTED]-cURL with user authentication in C#-geoserver

Accepted answer
Score: 12

HTTP Basic authentication requies everything 1 after "Basic " to be Base64-encoded, so try

request.Headers["Authorization"] = "Basic " + 
    Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
Score: 10

The solution to my question was changing 4 the ContentType property. If I change the ContentType 3 to

request.ContentType = "text/xml";

the request works in both cases, if I 2 also convert the authInfo to a Base64String in the 1 last example like Anton Gogolev suggested.

Score: 2

Using:

request.ContentType = "application/xml";

request.Credentials = new NetworkCredential(GEOSERVER_USER, GEOSERVER_PASSWD);

also works. The second sets authentication 1 information.

Score: 0

Or, if you want to use HttpClient:

 var authValue = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes("admin:geoserver")));
        try
        {
            var client = new HttpClient()
            {
                DefaultRequestHeaders = { Authorization = authValue }
            };

            string url = "http://{BASE_URL}";
            client.BaseAddress = new Uri(url);

            var content = new StringContent("<workspace><name>TestTestTest</name></workspace>",
                 Encoding.UTF8, "text/xml");
            var response = await client.PostAsync($"/{PATH_TO_API}/", content);
            response.EnsureSuccessStatusCode();
            var stringResponse = await response.Content.ReadAsStringAsync();
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine(ex.Message);
        }

0

More Related questions