如何根据小程序的请求设置 Accept-Language 标头

发布于 2024-08-09 20:32:28 字数 206 浏览 1 评论 0原文

我不熟悉 Java,但我需要从我的小程序中向远程 Web 服务发出请求。

Web 服务 (.Net 1.1) 使用 HttpContext.Current.Request.UserLanguages[0] 来确定要使用的语言。但该成员的值始终为空。

那么有没有办法传递 Accept-Language 标头以及“en-GB”集之类的内容?

I'm not familiar with Java but I need to make a request to a remote webservice from within my applet.

The webservice (.Net 1.1) uses HttpContext.Current.Request.UserLanguages[0] to determine the language to use. But the value of this member is alway null.

So is there a way to pass the Accept-Language header along with something like "en-GB" set?

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

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

发布评论

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

评论(3

滥情空心 2024-08-16 20:32:28

[新答案]

好的,我假设您在小程序中执行了类似的操作

URL url = new URL("http://www.whateverwebservice.com/passmealongthedata");
URLConnection urlconn = url.openConnection();

然后只需在执行真正的请求之前设置 Accept-Language 标头

//Assuming you know the language parameter you want to pass along you
urlconnection.setRequestProperty("Accept-Language", "en-GB");
//or "en-GB,en;q=0.7" or similar
....
continue with your program flow
....

如果语言参数应取决于浏览器中设置的参数,使用 .Net 方法是有意义的。当用户请求页面时,在页面上构建小程序,插入下面描述的附加 标记。并修改小程序以发送该值。希望我对此很清楚。


[已删除]


[旧答案]

假设您确实想确定小程序中客户端的浏览器版本:

这从 java AFAIK 中不可能直接实现,因为小程序不必关心它在哪个浏览器中运行。但是您可以

  • 使用 javascript 首先使用 javascirpt 确定浏览器版本,
  • 然后动态编写小程序标签
  • 并通过标签将浏览器版本传递到小程序中

检查 将参数传递给小程序,获取有关如何执行此操作的示例。

[NEW ANSWER]

Ok I assume you do something like this in the applet

URL url = new URL("http://www.whateverwebservice.com/passmealongthedata");
URLConnection urlconn = url.openConnection();

Then just set the Accept-Language header before you do the real request

//Assuming you know the language parameter you want to pass along you
urlconnection.setRequestProperty("Accept-Language", "en-GB");
//or "en-GB,en;q=0.7" or similar
....
continue with your program flow
....

If the language parameter should depend on the one set in the browser, it would make sense to use your .Net approach. When the user requests the page with the applet on construction on the page insert the below described additional <parameter> tag. And modify the applet to send that value along. Hope I'm clear on this.


[REMOVED]


[OLD ANSWER]

Assuming you really want to determine the browser version on the client side in an applet:

That isn't directly possible from java AFAIK as the applet shouldn't have to care in which browser it is running. But you could

  • with javascript first determine the browser version
  • with javascirpt then dynamically writing the applet tag
  • and passing the browser version in to the applet via a tag

Check Passing Parameters to Applets for a sample on how to do this.

小情绪 2024-08-16 20:32:28

如果您足以了解小程序运行的系统的默认语言(操作系统),您可以从Applet#getLocale()获取它。如果您确实需要首选浏览器语言,您可以从 ServletRequest#getLocale() 在 servlet 容器中的服务器端获取它,并动态生成 applet 标记,将语言代码作为参数传递给 applet 。

If it is good enough for you to know the default language (operating system) of the system the applet is running, you can get it from Applet#getLocale(). If you really need the preferred browser language, you can get it server-side in a servlet container from ServletRequest#getLocale() and generate the applet tag dynamically, passing the language code to the applet as a parameter.

初懵 2024-08-16 20:32:28

这可能会查看 Accept-Language HTTP 标头,您可以通过以下方式在 Java 中获取该标头:

request.getHeader("Accept-Language")

This probably looks at the Accept-Language HTTP header, which you can get in Java by doing

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