WebRequest C# 上的 curl 凭据

curl credentials on WebRequest C#

我正在尝试写这个电话

curl -d @credentials.json -H "Content-Type: application/json" http://128.136.179.2:5000/v2.0/tokens

使用 WebRequest。

我不确定我应该如何指示凭据:

{"auth":{"passwordCredentials":{"username": "user", "password": "pass"},"tenantName": "tenant"}}

现在,我正在这样做:

WebRequest request = WebRequest.Create(serverUrl + "tokens");
request.ContentType = "application/json";
request.Method = "GET";
string authInfo = "passwordCredentials:{username:" + username + ", password:" + password + "},tenantName:" + tenantname;
request.Headers["Authorization"] = "Basic " + authInfo;
WebResponse response = request.GetResponse();

谢谢!

您的 curl 只是将数据发送到该主机。它不会将其添加到 header。你应该在 c#

中做同样的事情
var request = WebRequest.Create(url);
request.ContentType = "application/json";
request.Method = "POST"; // I assume your token server accept post request for tokens? It not change for right verb.
new StreamWriter(request.GetRequestStream()).Write(jsondata);// just for demo. you should also close writer.
request.ContentLength = jsondata.Length;
var response = request.GetResponse();

关于如何使用request的更多信息你可以查看msdn https://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx