Note that there are some explanatory texts on larger screens.

plurals
  1. POcURL with user authentication in C#
    primarykey
    data
    text
    <p>I want to do the following cURL request in c#:</p> <pre><code>curl -u admin:geoserver -v -XPOST -H 'Content-type: text/xml' \ -d '&lt;workspace&gt;&lt;name&gt;acme&lt;/name&gt;&lt;/workspace&gt;' \ http://localhost:8080/geoserver/rest/workspaces </code></pre> <p>I have tried using a WebRequest:</p> <pre><code>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("&lt;workspace&gt;&lt;name&gt;my_workspace&lt;/name&gt;&lt;/workspace&gt;"); Stream reqstr = request.GetRequestStream(); reqstr.Write(buffer, 0, buffer.Length); reqstr.Close(); WebResponse response = request.GetResponse(); ... </code></pre> <p>But I get an error: (400) Bad request.</p> <p>If I change the request credentials and add the authentication in the header:</p> <pre><code>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("&lt;workspace&gt;&lt;name&gt;my_workspace&lt;/name&gt;&lt;/workspace&gt;"); Stream reqstr = request.GetRequestStream(); reqstr.Write(buffer, 0, buffer.Length); reqstr.Close(); WebResponse response = request.GetResponse(); ... </code></pre> <p>Then I get: (401) Unauthorised.</p> <p>My question is: Should I use another C# class like WebClient or HttpWebRequest or do I have to use the curl bindings for .NET?</p> <p>All comments or guidance would be appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload