Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
522 views
in Technique[技术] by (71.8m points)

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

There is a code that creates new featureType at geoserver:

string par = @"/c D:curl-7.32.0-ssl-sspi-zlib-static-bin-w32curl.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:WindowsSystem32cmd.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).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...