How to set Accept-Language header on request from applet
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 HttpC开发者_运维技巧ontext.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?
[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.
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.
This probably looks at the Accept-Language
HTTP header, which you can get in Java by doing
request.getHeader("Accept-Language")
精彩评论