如何在java中发送HTTP标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/819474/
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
How to send an HTTP header in java
提问by Simonw
Is it possible to send a Http header via a URL connection in java? I had this working using sockets, but ran into issues with a firewall which don't seem to be a problem with URLConnection. From looking at the API I get the impression that the output methods in URLConnection are just for filling in forms etc, or can they be used to send my own HTTP headers?
是否可以通过 java 中的 URL 连接发送 Http 标头?我使用套接字进行了这项工作,但遇到了防火墙问题,这似乎不是 URLConnection 的问题。从查看 API 我得到的印象是 URLConnection 中的输出方法仅用于填写表单等,或者它们可以用于发送我自己的 HTTP 标头吗?
采纳答案by Matthew Flaschen
Yes, the method you want is setRequestProperty.
是的,您想要的方法是setRequestProperty。
回答by HaBaLeS
The method you want is setRequestProperty, but I would recommend to use Apaches HttpClient. With this library you have total control over the request you want to send.
您想要的方法是 setRequestProperty,但我建议使用 Apaches HttpClient。使用此库,您可以完全控制要发送的请求。