2012-02-28 3 views
0

나는 학생이며 Azure에서 호스트 된 서비스의 인스턴스 카운트를 사용자가 변경할 수있는 응용 프로그램을 만들려고합니다. 이것은 서비스 (http://msdn.microsoft.com/en-us/library/windowsazure/ee460809.aspx)에 대한 새로운 구성 파일을 업로드하는 것입니다. 내 문제는 내가 오류 "원격 서버 오류를 반환 계속 : (403) 금지"내가 아래의 코드에서 응답을 얻으려고 할 때. 나는 오류가 인증서와 관련이 있어야한다고 생각하지만 수행 할 수 있습니다. GET 요청을 성공적으로 수행하고 여기에서 사용하는 것과 동일한 인증서를 사용하여 올바른 응답을받습니다. 어떤 도움도 크게 apprec.config는 새로운 구성 파일입니다. 콘텐츠 유형이 잘못처럼Azure 서비스 관리 API 변경 구성

공공 무효 changeConfiguration (문자열 서비스 명, 문자열 deploymentSlot, 캐릭터 설정, 문자열 deploymentName는)

{ 

     byte[] encodedConfigbyte = new byte[config.Length]; 
     encodedConfigbyte = System.Text.Encoding.UTF8.GetBytes(config); 
     string encodedConfig = Convert.ToBase64String(encodedConfigbyte); 

     Uri changeConfigRequestUri = new Uri("https://management.core.windows.net/" + subscriptionId + "/services/hostedservices/" + serviceName + "/deployments/" + deploymentName + "/?comp=config)"); 

     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(changeConfigRequestUri); 

     request.Headers.Add("x-ms-version", "2010-10-28"); 
     request.Method = "POST"; 

     string bodyText = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
          "<ChangeConfiguration xmlns=\"http://schemas.microsoft.com/windowsazure" + ">" + "<Configuration>" + encodedConfig + "</Configuration>" + "<TreatWarningsAsError>false</TreatWarningsAsError>" + "<Mode>Auto</Mode>"+"</ChangeConfiguration>"; 

     byte[] buf = Encoding.UTF8.GetBytes(bodyText); 
     request.ContentType = "text/xml"; 
     request.ContentLength = buf.Length; 

     StreamWriter writer = new StreamWriter(request.GetRequestStream()); 
     var data = Encoding.ASCII.GetBytes(buf.ToString()); 
     writer.Write(data); 
     writer.Flush(); 
     writer.Close(); 

     X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
     try 
     { 
      certStore.Open(OpenFlags.ReadOnly); 
     } 
     catch (Exception e) 
     { 
      if (e is CryptographicException) 
      { 
       Console.WriteLine("Error: The store is unreadable."); 
      } 
      else if (e is SecurityException) 
      { 
       Console.WriteLine("Error: You don't have the required permission."); 
      } 
      else if (e is ArgumentException) 
      { 
       Console.WriteLine("Error: Invalid values in the store."); 
      } 
      else 
      { 
       throw; 
      } 
     } 


     X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false); 
     certStore.Close(); 

     if (certCollection.Count == 0) 
     { 
      throw new Exception("Error: No certificate found containing thumbprint " + thumbprint); 
     } 

     X509Certificate2 certificate = certCollection[0]; 
     request.ClientCertificates.Add(certificate); 


     //Error occurs in line below 
     WebResponse response = (HttpWebResponse)request.GetResponse(); 
     try 
     { 
      response = request.GetResponse(); 
     } 
     catch (WebException e) 
     { 
      string test = e.Message; 
     } 

답변

0

것 같습니다. 이는 API 호출에 대한 요구 사항 중 하나입니다.

당신은 유무 :

request.ContentType = "text/xml"; 

당신이 있어야합니다 또한

request.ContentType = "application/xml"; 

을, 당신을 "2010-10-28"대신 X-MS-버전을 설정하는 이유가있다 최신 API의 "2011-08-01";

참조 : http://msdn.microsoft.com/en-us/library/windowsazure/ee460809.aspx

편집 : 나는 또한 당신이 당신의 신체 데이터를 추가하는 방법에 약간의 걱정. 텍스트를 UTF8 바이트 배열로 변환 한 다음 바이트 배열에서 tostring을 수행하고 아스키 바이트 배열로 다시 인코딩하는 것처럼 보입니다. 이것은 나에게 이상하다. 문자열을 StreamWriter의 Write (bodyText) 메서드에 직접 전달할 수 있어야합니다. API는 전송중인 본문 데이터를 디코딩하는 방법을 알지 못할 수 있습니다.

은 ... 이런 식으로 뭔가를 시도

request.ContentType = "application/xml"; 

    using (var writer = new StreamWriter(request.GetRequestStream())) 
    { 
     writer.Write(bodyText); 
    } 
관련 문제