IM 尝试使用 Windows Azure 服务管理 REST API 参考以编程方式添加管理证书
以下是使用的片段
public async void AddManagmentCertificate(string subscriptionId, string version)
{
string hittingurl = "https://management.core.windows.net/{0}/certificates";
hittingurl = String.Format(hittingurl, "subidgoeshere");
XmlDocument addcertDoc = await HelperMethods.PostXmlDocument(hittingurl,"2012-03-01");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(addcertDoc.NameTable);
nsmgr.AddNamespace("rest", xmlnamespace);
if (addcertDoc != null)
{
XmlNodeList certadded = addcertDoc.SelectNodes("//rest:SubscriptionCertificateThumbprint", nsmgr);
if (certadded.Count > 0)
{
string thumbprint = certadded[0].InnerXml.ToString();
if (String.IsNullOrEmpty(thumbprint))
{
}
else
{
LoadData();
}
}
}
}
public static async Task<XmlDocument> PostXmlDocument(string hittingUrl,string Version)
{
HttpWebRequest request;
XmlDocument responsebody = new XmlDocument();
Uri uri = new Uri(hittingUrl);
request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = "POST";
request.Headers.Add("x-ms-version", Version);
string certpath = HttpContext.Current.Server.MapPath(@"Certificates\epgwin8dashboard.cloudapp.net.cer");
request.ClientCertificates.Add(X509Certificate2.CreateFromCertFile(certpath));
request.ContentType = "application/xml";
request.ContentLength = 0;
HttpWebResponse webresponse = null;
try
{
webresponse = (HttpWebResponse)await request.GetResponseAsync();
}
catch (Exception)
{
}
HttpStatusCode statuscode = webresponse.StatusCode;
if (webresponse.ContentLength > 0)
{
using (XmlReader reader = XmlReader.Create(webresponse.GetResponseStream()))
{
responsebody.Load(reader);
}
}
if (statuscode.Equals(HttpStatusCode.OK))
{
return responsebody;
}
else
{
return null;
}
}
请告诉我还有什么需要改进才能使这个工作没有错误
我被 411 错误返回。