WebRequest C#上的curl凭据

时间:2016-04-26 18:41:34

标签: c# .net curl webrequest

我正在尝试写这个电话

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

谢谢!

1 个答案:

答案 0 :(得分:1)

您的curl只是将数据发送到该主机。它没有将它添加到标题中。你在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();

有关如何使用请求的更多信息,请查看msdn https://msdn.microsoft.com/en-us/library/debx8sh9(v=vs.110).aspx