Tôi đã có nhiều cho các API quản lý Azure để làm việc với mã bên dưới ngoại trừ GetRole cho Máy ảo. Dưới đây là doc cho cuộc gọi api này: https://msdn.microsoft.com/en-us/library/azure/jj157193.aspxNhận vai trò - Azure API trả về (400) Yêu cầu không hợp lệ
Dưới đây là đoạn code mà tôi đang cố gắng để thực hiện:
static void Main(string[] args)
{
Program p = new Program();
p.MakeRequest();
}
public void MakeRequest()
{
string strThumbprint = "{thumbprint}";
X509Certificate2 certificate = GetStoreCertificate(strThumbprint);
string strRequestURI = "https://management.core.windows.net/{subscription}/services/hostedservices/{cloud-service}/deployments/{deployment}/roles/{rolename}";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(strRequestURI);
request.ClientCertificates.Add(certificate);
request.ContentType = "application/xml";
request.Headers.Add("x-ms-version", "2015-04-01");
try
{
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Status Code: " + response.StatusCode.ToString());
Stream receiveStream = response.GetResponseStream();
Encoding encode = Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, encode);
Console.WriteLine(readStream.ReadToEnd());
response.Close();
}
catch(Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
private static X509Certificate2 GetStoreCertificate(string thumbprint)
{
List<StoreLocation> locations = new List<StoreLocation>
{
StoreLocation.CurrentUser,
StoreLocation.LocalMachine
};
foreach (var location in locations)
{
X509Store store = new X509Store("My", location);
try
{
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection certificates = store.Certificates.Find(
X509FindType.FindByThumbprint, thumbprint, false);
if (certificates.Count == 1)
{
return certificates[0];
}
}
finally
{
store.Close();
}
}
throw new ArgumentException(string.Format(
"A Certificate with Thumbprint '{0}' could not be located.",
thumbprint));
}
EDIT: Bây giờ tôi đã cố định URL nhưng tôi nhận được 400 - Bad Request.
Bạn có thể đăng thông báo lỗi ở đây ngoài chỉ có mã trạng thái không? – juvchan
Tin nhắn tôi nhận được là: "Máy chủ từ xa đã trả về lỗi: (404) Không tìm thấy." – Jeremy
400 - Yêu cầu không hợp lệ, Tin nhắn HTTP bạn gửi không đúng định dạng. Có thể có nhiều lý do cho việc này. Bạn có theo dõi bằng trình gỡ lỗi bảng điều khiển web không? – Ian