5

How can I spoof the user agent of a JavaScript GET request? setRequestHeader with User-Agent isn't allowed:

xmlHttpRequest.setRequestHeader("User-Agent", "...");
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
XP1
  • 6,910
  • 8
  • 54
  • 61
  • I hop that this wont work you shuld not be able to tell the server that your in an other env then you are from a client. – megakorre Aug 09 '11 at 11:21
  • 3
    You can and you should be able to tell the server that you're in a different enviroment than you actually are - and plenty of browsers do allow you o change your User-Agent string. That beeing said I do not know of such a mechanism controllable from JavaScript. – Daniel Baulig Aug 09 '11 at 11:25
  • On the client-side browser, I need to get the webpage source from a server that performs server-side browser sniffing. Any other ideas? – XP1 Aug 09 '11 at 11:35

2 Answers2

5

In short: You can't due to built-in cross-domain limitations.

One way "around" that was to write a proxy-webservice and let the server spoof whatever headers you need spoofed.

erlando
  • 6,736
  • 4
  • 24
  • 29
4

You can't do this in a half-decent browser because of security issues surrounding it. You don't want XSS scripts to be changing request headers and running rampant on your site.

However I believe there's a workaround in IE if you use VBScript:

MyHttp.setRequestHeader "User-Agent", "MyCustomUser"

The alternative is to have a web page on your site dedicated to forwarding a GET request, changing the appropriate headers as necessary.

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
tskuzzy
  • 35,812
  • 14
  • 73
  • 140