在C#中卷曲-u等价物?

时间:2016-05-04 17:19:35

标签: c# curl

我无法找到一个有效的解决方案来将cur中的-u'token'param传递给后续的Web调用。我已经尝试过HttpClient,WebClient,Flurl,但无法破解如何使用令牌正确格式化登录后请求。任何人都可以给我一个c#例子的例子,相当于curl的-u?

这是来自Barracuda Load Balancer API DOC

登录请求

$ curl -X POST \ -H "Content-Type:application/json" \ -d '{"username": "admin", "password": "admin"}' \ http://10.11.19.104:8000/restapi/v2/login  

Response:  
{"token":"eyJldsI6IjE0NjIzODQ0MTgiLCJwYXNzd29yZCI6ImUzZDFhY2IwNzlkZmQxMDZmMmRhODEwZGRm
MzFiNGI3IiwidXNlciI6InNlYW4ifQ=="} 

退出请求

$ curl -u 'J3sidXNlcm5hbWUiOiJ5Jw=\n:' \ -X DELETE \ http://10.11.19.104:8000/restapi/v2/logout  

Response: { "msg": "Success" }

这是我尝试使用令牌:

using System;
using System.Net;
using System.IO;
using System.Text;
using System.Web.Helpers;
using Newtonsoft.Json;
using NLog;

namespace Barracuda
{
    class Program
    {

        private static Logger logger = LogManager.GetCurrentClassLogger();
        public static String BaseUrl = "https://load.balancer.ip";
        public static String RestApi = "restapi/v2/";
        public static String LoginEndpoint = RestApi + "login";
        public static String LogoutEndpoint = RestApi + "logout";
        public static String VirtualUserGroups = RestApi + "virtual_service_groups";

        public class LoginCreds
        {
            public string username { get; set; }
            public string password { get; set; }
        }

        public class LogoutMessage
        {
            public string msg { get; set; }
        }

        public class AuthToken
        {
            public string token { get; set; }
        }

        static void Main(string[] args)
        {
            //login
            ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;
            var request = (HttpWebRequest) WebRequest.Create(BaseUrl + "/" + LoginEndpoint);
            request.ContentType = "application/json";
            request.Method = "POST";
            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                var creds = new LoginCreds {username = "myUsername", password = "myPassword"};
                string json = JsonConvert.SerializeObject(creds);
                streamWriter.Write(json);
            }
            var response = (HttpWebResponse) request.GetResponse();
            AuthToken myToken;
            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                myToken = JsonConvert.DeserializeObject<AuthToken>(streamReader.ReadToEnd());
                logger.Log(LogLevel.Debug, myToken.token);
            }
            //logout
            request = (HttpWebRequest) WebRequest.Create(BaseUrl + "/" + LogoutEndpoint);
            request.ContentType = "application/json";
            request.Method = "DELETE";
            request.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(myToken.token));
            response = (HttpWebResponse) request.GetResponse();
            using (var streamReader = new StreamReader(response.GetResponseStream()))
            {
                 var logoutMessage = JsonConvert.DeserializeObject<LogoutMessage>(streamReader.ReadToEnd());
                logger.Log(LogLevel.Debug, logoutMessage.msg);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

FML - 错字。在Basic之后需要标题中的空格。