2011-08-15 5 views
3

인증이 필요하고 운이 필요없는 REST 소스를 읽는 방법을 알아 내려고하고 있습니다. 나는 다음과 같이 작업을 잘 사용하여 C 번호를 가지고 :Java를 사용하여 REST 서비스에서 읽기

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(filename); 
request.Accept = "application/xml"; 
request.ContentType = "application/xml"; 
request.KeepAlive = true; 

// this part is not used until after a request is refused, but we add it anyways 
CredentialCache myCache = new CredentialCache(); 
myCache.Add(new Uri(filename), "Basic", new NetworkCredential(username, password)); 
request.Credentials = myCache; 

// this is how we put the uname/pw in the first request 
string cre = String.Format("{0}:{1}", username, password); 
byte[] bytes = Encoding.ASCII.GetBytes(cre); 
string base64 = Convert.ToBase64String(bytes); 
request.Headers.Add("Authorization", "Basic " + base64); 

HttpWebResponse response = (HttpWebResponse) request.GetResponse(); 
return response.GetResponseStream(); 

을하지만 자바 다음 작동하지 않습니다 :

URL url = new URL(dsInfo.getFilename()); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setRequestMethod("GET"); 
conn.setRequestProperty("Accept", "application/xml"); 
conn.setRequestProperty("Content-Type", "application/xml"); 
BASE64Encoder encoder = new BASE64Encoder(); 
String encodedCredential = encoder.encode((dsInfo.getUsername() + ":" + dsInfo.getPassword()).getBytes()); 
conn.setRequestProperty("Authorization", "BASIC " + encodedCredential); 

conn.connect(); 

InputStream responseBodyStream = conn.getInputStream(); 

를 스트림이 반환 :

Error downloading template 

Packet: test_packet 
Template: NorthwindXml 

Error reading authentication header. 

을 내가 잘못 무엇을 얻고있다 ?

감사 - 데이브

+0

"작동하지 않음"은 무엇을 의미합니까? Wireshark를 사용하여 두 방법간에 네트워크의 차이점을 확인해 보셨습니까? –

+0

OOPS - 죄송합니다. 위의 결과를 추가했습니다. –

+0

서버가 RFC 2617을 올바르게 구현하고 있는지 확실합니까? 예를 들어 auth-scheme 토큰은 대소 문자를 구분합니다. –

답변

2

사용자 이름/암호의 인코딩으로는 :

은 자바) UTF-8 인코딩, getBytes를 (사용하는 로컬 호스트 인코딩에 해당하는 바이트를 반환 (사람이있을 수 있습니다 여부를 ASCII). javadoc of String은 더 자세한 정보를 제공합니다.

인코딩 된 문자열의 값을 C# 및 Java에서 모두 인쇄하고 일치하는지 확인하십시오.

1

대답은 Codo에서 기본 - 기본 대신 기본 -에서 왔습니다.

관련 문제