如何用HttpClient对象调用或一些类似的替换curl调用? [英] How do I replace curl invoke with HttpClient object invoke or some similar?

查看:1293
本文介绍了如何用HttpClient对象调用或一些类似的替换curl调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个代码可以在geoserver中创建新的featureType:

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

我想读取服务器的输出并处理错误,他们说我应该用 HttpClient ,但我不知道如何描述授权( -u admin:MYPASSWORD )。

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

推荐答案

它看起来像基本的auth,所以这应该这样。

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

这篇关于如何用HttpClient对象调用或一些类似的替换curl调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆