2016-10-04 3 views
1

I가 키를 반환하는 서비스를 통합하고있어 때 다음과 같은 형식에있는 URL에 GET 요청 :HttpClient를 액세스 URL '@'(기호)

https://username:[email protected]/refresh.key 

나는 액세스 브라우저에서 URL, 그것은 내가 그 URL에 '@' 함께 할 수있는 뭔가가 생각 HttpClient가 나는 401

HttpClient _client = new HttpClient(); 
var response = await _client.GetAsync(@"https://username:[email protected]/refresh.key"); // Returns a 401 

을 얻을 사용하여 GET 요청을 할 경우에 예상대로 새 키를 반환하지만 그것을 고칠 방법이 확실하지 않다, 나는 그것을 '%40'으로 바꾸려고 노력했다. 그러나 내가 그쪽을 할 때 나는 UriFormatException을 얻는다.

누구든지이 작업을 수행하는 방법을 알고 있습니까?

+0

URI를 작동 : password' 자격 증명을 기본 인증을 사용하여 요청으로 변환해야합니다 http://www.baeldung.com/httpclient-4-basic-authentication – Dai

답변

1

당신은 HttpClient를의, 당신은 아래의 코드를 시도 할 수 있습니다 인증 헤더를 수정해야합니다;

HttpClient _client = new HttpClient(); 
byte[] usernamePasswordBytes = Encoding.ASCII.GetBytes("user:pass"); 
_client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(usernamePasswordBytes)); 
var response = await _client.GetAsync(@"https://service.com/refresh.key"); 

추신 : 이러한 사용자 이름 : [email protected] 요청은 BasicAuthentication 요청이므로 실제로 기본 인증 요청을 시도합니다.

희망이 이름`와 당신

1

URL에 자격 증명을 제공 할 필요가 없습니다. 대신 당신은 할 수 있습니다 :

using (var handler = new HttpClientHandler {Credentials = new NetworkCredential("username", "password")}) { 
    using (HttpClient _client = new HttpClient(handler)) { 
      var response = await _client.GetAsync(@"https://service.com/refresh.key"); 
    } 
}