How do I replace curl invoke with HttpClient object invoke or some similar?

你。 提交于 2019-12-24 23:18:39

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!