2011-08-15 7 views
3

RESTソースから、認証が必要であり、運がない方法を読み取る方法を理解しようとしています。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(); 

しかし、Javaは次のために働いていない:

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. 

何を取得しています私はこの次のようにC#を使用して正常に動作しています?

感謝 - ユーザー名/パスワードのエンコーディングでデイブ

+0

「機能しない」とはどういう意味ですか?あなたはWiresharkを使って2つの方法の間でネットワーク上の違いを確認しようとしましたか? –

+0

OOPS - 申し訳ありません。上記の結果を追加しました。 –

+0

サーバがRFC 2617を適切に実装していることは、どの程度ですか?たとえば、auth-schemeトークンは大文字小文字を区別していることがわかりますか? –

答えて

2

Javaは(UTF-8エンコーディングを使用し、GetBytesメソッド)は、ASCIIであってもなくてもよい人(ローカルホストのエンコードに対応するバイトを返します)。 javadoc of Stringはより詳細な情報を提供します。

エンコードされた文字列の値をc#とJavaの両方に出力し、一致するかどうかを確認します。

1

答えは基本的なものではなく、基本的なコメントであるCodoから来ています。

関連する問題