2014-01-23 47 views
0

我試圖讓從RWS的請求,我經常收到的401 not authorized error使用基本身份驗證

我已經提供了用於測試的樣本的用戶名和密碼,但我有一種感覺,連接到一個RESTful Web服務我的實現關閉了。這是我第一次進入RWS世界。

class Program 
    { 
     static string _address = "https://this.istheservice/"; 

     static async void RunClient() 
     { 
      HttpClientHandler handler = new HttpClientHandler(); 

      handler.UseDefaultCredentials = false; 

      handler.Credentials = new NetworkCredential("Sample", "[email protected]!!"); 

      HttpClient client = new HttpClient(handler); 

      // Send asynchronous request 
      HttpResponseMessage response = await client.GetAsync(_address); 

      // Check that response was successful or throw exception 
      response.EnsureSuccessStatusCode();//Breaks here 

當我檢查了域名被列出的NetworkCredentials爲Domain = ""這將導致一個問題?

如果我放棄它們提供給瀏覽器的URL,我也會收到401錯誤。

如果需要更多信息,請詢問,我不確定在這裏需要包含哪些運動部件。

我正在從這個例子中工作:http://blogs.msdn.com/b/henrikn/archive/2012/02/17/downloading-a-google-map-to-local-file.aspx,我正在使用Fiddler來監視請求。

回答

0

你想做什麼沒有基本的身份驗證。 做基本的身份驗證,你需要爲Base64加密的用戶名:密碼,並把它們在頭

string basic = "Basic ENCRYPTEDSTRING"; 
    HttpClient client = new HttpClient(); 
    client.DefaultRequestHeaders.Add("Authorization", basic); 

所以,你的頭應該有這樣的事情,當你看到它在提琴手

授權:基本QWxhZGRpbjpvcGVuIHNlc2FtZQ ==

維基鏈接到基本身份驗證http://en.wikipedia.org/wiki/Basic_access_authentication

+0

會這樣'字符串基本=「基本ENCRYPTEDSTRING」'是一些客戶端將提供給我或代碼ABOV會堵e在我的應用程序中工作? –

+0

在你的例子中你會base64加密「Sample = P @ ssw0rd !!」這是U2FtcGxlPVBAc3N3MHJkISE = –

+0

你可以使用在線工具,如果你想http://www.opinionatedgeek.com/dotnet/tools/base64encode/ –