使用基本身份验证ASP .NET MVC发送GET服务

时间:2013-11-21 18:17:28

标签: asp.net-mvc web-services httpclient basic-authentication

我想将GET请求发送到使用基本身份验证保护的服务(因此地址为:https ...)。 我怎么能在MVC中做到这一点?如何将登录名和密码放入要通过此服务进行身份验证的请求中?

1 个答案:

答案 0 :(得分:0)

这就是我的预期,并最终得到它:

        string url = @"https://api2.orange.pl/sendsms/?from=509802111&to=509802111&msg=xyz";
        WebClient client = new WebClient();

        string userName = "LLLL";
        string password = "PPPP";

        string credentials = Convert.ToBase64String(Encoding.Default.GetBytes(userName + ":" + password));
        client.Headers["Authorization"] = "Basic " + credentials;

        //to ignore certificate error (fix from StackOverflow)
        ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
        var result = client.DownloadString(url);