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