使用 Java 从 REST 服务读取

发布于 2024-11-29 11:29:15 字数 1662 浏览 1 评论 0原文

我试图弄清楚如何从需要身份验证的 REST 源中读取数据,但运气不佳。我使用 C# 可以正常工作,如下所示:

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.

我错了什么?

谢谢-戴夫

I am trying to figure out how to read from a REST source that requires authentication and having no luck. I have this working fine using C# as follows:

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();

But for Java the following is not working:

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();

The stream is returning:

Error downloading template

Packet: test_packet
Template: NorthwindXml

Error reading authentication header.

What am I getting wrong?

thanks - dave

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独陪着我 2024-12-06 11:29:15

在用户名/密码的编码中:

Java 使用 UTF-8 编码,并且 getBytes() 返回与本地主机编码(可能是或不是 ASCII)相对应的字节。 String 的 javadoc 为您提供更多详细信息。

在 C# 和 Java 中打印此类编码字符串的值并检查它们是否匹配。

In your encoding of username/password:

Java uses UTF-8 encoding, and getBytes() returns the bytes corresponding to the local host encoding (who may be or not ASCII). The javadoc of String gives you more detail.

Print the values of such encoded Strings both in c# and Java and check if they match.

静谧 2024-12-06 11:29:15

答案来自 Codo 在评论中 - Basic 而不是 BASIC。

The answer came from Codo in a comment - Basic instead of BASIC.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文