我想要做的在C#中,下列curl请求:卷曲在C#用户认证
curl -u admin:geoserver -v -XPOST -H 'Content-type: text/xml' \
-d '<workspace><name>acme</name></workspace>' \
http://localhost:8080/geoserver/rest/workspaces
我一直在使用一个WebRequest的尝试:
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("<workspace><name>my_workspace</name></workspace>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();
WebResponse response = request.GetResponse();
...
但我得到一个错误:(400)错误的请求。
如果我更改请求凭据,并在头添加认证:
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("<workspace><name>my_workspace</name></workspace>");
Stream reqstr = request.GetRequestStream();
reqstr.Write(buffer, 0, buffer.Length);
reqstr.Close();
WebResponse response = request.GetResponse();
...
然后我得到:(401)未经授权。
我的问题是:我应该使用另一个类似WebClient或HttpWebRequest的C#类,还是必须使用.NET的curl绑定?
所有评论或指导,将不胜感激。
我试图转换AUTHINFO为BAse64String但后来我得到了“(400)错误的请求错误”。 – taudorf 2011-03-01 11:47:37
@taudorf:嗯......下载小提琴手,看看两大POST请求之间的区别。 – 2011-03-01 11:52:06