cURL with user authentication in C#
Asked Answered
H

4

11

I want to do the following cURL request in c#:

curl -u admin:geoserver -v -XPOST -H 'Content-type: text/xml' \
   -d '<workspace><name>acme</name></workspace>' \
   http://localhost:8080/geoserver/rest/workspaces

I have tried using a WebRequest:

string url = "http://localhost:8080/geoserver/rest/workspaces";
WebRequest request = WebRequest.Create(url);

request.ContentType = "Content-type: text/xml";
request.Method = "POST";
request.Credentials = new NetworkCredential("admin", "geoserver");

byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>my_workspace</name></workspace>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();

WebResponse response = request.GetResponse();
...

But I get an error: (400) Bad request.

If I change the request credentials and add the authentication in the header:

string url = "http://localhost:8080/geoserver/rest/workspaces";
WebRequest request = WebRequest.Create(url);

request.ContentType = "Content-type: text/xml";
request.Method = "POST";
string authInfo = "admin:geoserver";
request.Headers["Authorization"] = "Basic " + authInfo;

byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes("<workspace><name>my_workspace</name></workspace>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();

WebResponse response = request.GetResponse();
...

Then I get: (401) Unauthorised.

My question is: Should I use another C# class like WebClient or HttpWebRequest or do I have to use the curl bindings for .NET?

All comments or guidance would be appreciated.

Heman answered 1/3, 2011 at 9:39 Comment(0)
H
11

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

request.ContentType = "text/xml";

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

Heman answered 2/3, 2011 at 10:24 Comment(0)
I
13

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

request.Headers["Authorization"] = "Basic " + 
    Convert.ToBase64String(Encoding.ASCII.GetBytes(authInfo));
Impracticable answered 1/3, 2011 at 11:8 Comment(2)
I have tried converting authInfo to a BAse64String but then I get the "(400) Bad request error".Heman
@taudorf: Hmm... Download Fiddler and see what's the difference between two POST requests.Impracticable
H
11

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

request.ContentType = "text/xml";

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

Heman answered 2/3, 2011 at 10:24 Comment(0)
O
3

Using:

request.ContentType = "application/xml";

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

also works. The second sets authentication information.

Ovation answered 21/6, 2012 at 12:51 Comment(0)
D
1

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);
        }
Divagate answered 30/9, 2019 at 20:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.