How to set Accept-Language header on request from applet

谁都会走 提交于 2019-12-11 07:58:25

问题


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?


回答1:


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.




回答2:


[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.




回答3:


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

request.getHeader("Accept-Language")


来源:https://stackoverflow.com/questions/1672661/how-to-set-accept-language-header-on-request-from-applet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!