java android 中的 HttpURLConnection 不发送正确的 User-Agent 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1429981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HttpURLConnection in android doesn't send correct User-Agent header
提问by Lucas S.
I have found an issue. I have a server that uses User Agent header to identify the device that is connecting to it. But when i connect to the server using HttpURLConnection i get no User Agent header, but when i connect with the browser it sends the correct User Agent.
我发现了一个问题。我有一个服务器,它使用用户代理标头来识别连接到它的设备。但是当我使用 HttpURLConnection 连接到服务器时,我没有得到用户代理标头,但是当我与浏览器连接时,它会发送正确的用户代理。
For testing i am using an echo server that replies with the headers it found on the request.
为了进行测试,我使用了一个回显服务器,该服务器使用它在请求中找到的标头进行回复。
When i connect with browser i get: eg: User-Agent: Mozilla/5.0 (Linux; U; Android 1.5; en-fr; HTC Hero Build/CUPCAKE) AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2 Mobile Safari/525.20.1 Up.Link/6.3.1.20.06.3.1.20.0.
当我连接浏览器时,我得到:例如:用户代理:Mozilla/5.0(Linux;U;Android 1.5;en-fr;HTC Hero Build/CUPCAKE)AppleWebKit/528.5+(KHTML,如 Gecko)版本/3.1.2移动 Safari/525.20.1 Up.Link/6.3.1.20.06.3.1.20.0。
But when i connect by code with UrlConnection i get:
但是当我使用 UrlConnection 通过代码连接时,我得到:
User-Agent: UNAVAILABLE.
用户代理:不可用。
Does anyone know we i get different behaviors? How i can i connect in the same way the browser does?
有谁知道我们有不同的行为?我如何以与浏览器相同的方式进行连接?
EDIT:
编辑:
What i really need is not only the User-Agent header, i also need some special headers (x-up-subno actually). This header is added by the APN of the operator, but for some reason when i connect by code that headers are not added to the request.
我真正需要的不仅是 User-Agent 标头,我还需要一些特殊的标头(实际上是 x-up-subno)。这个标头是由运营商的 APN 添加的,但是由于某种原因,当我通过代码连接时,标头没有添加到请求中。
Thanks,
谢谢,
回答by Vineet Reynolds
The HTTP specificationstates that all clients are supposed to sent User-Agent headers. It however, does not state that they should identify the client in the manner a server wishes to. Android therefore complies with the specification, and there is not a lot you can do about it.
在HTTP规范规定,所有客户都应该发送的用户代理头。然而,它并没有说明他们应该以服务器希望的方式识别客户端。因此,Android 符合规范,您对此无能为力。
The best you could do is to use the setRequestProperty()method to attempt to get a preferable user-agent value in the request. There are no guarantees that it would work, but it is likely. The method needs to be called in the following manner:
您能做的最好的事情是使用setRequestProperty()方法来尝试在请求中获取更可取的用户代理值。不能保证它会起作用,但很有可能。该方法需要通过以下方式调用:
connection.setRequestProperty("User-Agent","MyAppName/1.0");
The stock Android browser uses WebKit. If you want to set WebKit's user-agent string, you'll either have to use a static value, or read the user-agent string from WebKit. I haven't attempted this, but the WebKit user-agent string is available in Android via the getUserAgentString() method of the WebSettings class.
股票 Android 浏览器使用 WebKit。如果要设置 WebKit 的用户代理字符串,则必须使用静态值,或者从 WebKit 读取用户代理字符串。我还没有尝试过,但是可以通过WebSettings 类的getUserAgentString() 方法在 Android 中使用 WebKit 用户代理字符串。
回答by Jeffrey Blattman
it's be wrong if HttpURLConnection faked the user agent of the browser. it's not the browser after all. it would be nice if it sent a little more information, like maybe "Java/1.6 (MacOS 10.5 ..." etc.
如果 HttpURLConnection 伪造浏览器的用户代理是错误的。毕竟它不是浏览器。如果它发送更多信息就好了,比如“Java/1.6 (MacOS 10.5 ...”等)。


