问题
There is a code that creates new featureType at geoserver:
string par = @"/c D:\curl-7.32.0-ssl-sspi-zlib-static-bin-w32\curl.exe -v -u admin:MYPASSWORD -XPOST -H ""Content-type: text/xml"" -d ""<featureType><name>" + name + @"</name><title>" + MyHtmlEncode(title) + @"</title></featureType>"" http://localhost:8080/geoserver/rest/workspaces/cite/datastores/postgis/featuretypes";
Process P = Process.Start(@"C:\Windows\System32\cmd.exe",par);
I want to read output of server and handle errors, they said I should replace curl with HttpClient
, but I don't know how to describe authorization (-u admin:MYPASSWORD
).
回答1:
It looks like basic auth, so this should do it.
var httpClient = new HttpClient();
var authHeader = new AuthenticationHeaderValue("basic", Convert.ToBase64String(Encoding.ASCII.GetBytes("admin:MYPASSWORD")));
httpClient.DefaultRequestHeaders.Authorization = authHeader;
var content = new StringContent("<featureType><name>" + name + @"</name><title>" + MyHtmlEncode(title) + @"</title></featureType>" );
content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
var response = await httpClient.PostAsync("http://localhost:8080/geoserver/rest/workspaces/cite/datastores/postgis/featuretypes", content);
来源:https://stackoverflow.com/questions/19532155/how-do-i-replace-curl-invoke-with-httpclient-object-invoke-or-some-similar