Java中HTTP请求中添加HEADER
我使用以下代码发送简单的 HTTP 请求:
try
{
Socket s = new Socket ();
s.bind (new InetSocketAddress (ipFrom, 0));
s.connect (new InetSocketAddress (ipTo, 80), 1000);
PrintWriter writer = new PrintWriter (s.getOutputStream ());
BufferedReader reader = new BufferedReader (new InputStreamReader (s.getInputStream ()));
writer.print ("GET " + szUrl + " HTTP/1.0\r\n\r\n");
writer.flush ();
s .close ();
reader.close ();
writer.close ();
}
但是,如您所见,我不发送自定义标头。 我应该添加什么来发送自定义标头?
I'm using this following code to send simple HTTP Request :
try
{
Socket s = new Socket ();
s.bind (new InetSocketAddress (ipFrom, 0));
s.connect (new InetSocketAddress (ipTo, 80), 1000);
PrintWriter writer = new PrintWriter (s.getOutputStream ());
BufferedReader reader = new BufferedReader (new InputStreamReader (s.getInputStream ()));
writer.print ("GET " + szUrl + " HTTP/1.0\r\n\r\n");
writer.flush ();
s .close ();
reader.close ();
writer.close ();
}
However, as you can see, I don't send a custom HEADER.
What should I add to send a custom HEADER ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不要尝试自己实现 HTTP 协议。
使用 Apache 的 HttpComponents。
(或其较旧且更易于使用的版本 - HttpClient)
Don't try to implement the HTTP protocol yourself.
Use HttpComponents by Apache.
(or its older and a little easier to use version - HttpClient)
当您写入
\r\n\r\n
位时,会发送一个换行/回车符来结束该行,然后发送另一个换行符/回车符来指示不再有标头。这是 HTTP 和电子邮件格式的标准,即空行表示标头的结束。为了添加额外的标头,您只需要在完成之前不要发送该序列。您可以执行以下操作When you write
The
\r\n\r\n
bit is sending a line-feed/carriage-return to end the line and then another one to indicate that there are no more headers. This is a standard in both HTTP and email formats, i.e. a blank line indicates the end of headers. In order to add additional headers you just need to not send that sequence until you're done. You can do the following instead即使我建议尝试 Bozho 提到的 HttpComponents 而不是自己实现 HTTP,这也是添加自定义标头的方法:
Even if I suggest to try HttpComponents as mentioned by Bozho instead of implementing HTTP by yourself, this is would be the way to add a custom header:
您应该使用已经准备好用于 http 连接的类,例如
HTTPUrlConnection
,它是UrlConnection
的子级,并且具有用于设置请求参数的方法(像 HEADER 字段)..检查 此处供参考
You should use classes already prepared to be used for http connections, like
HTTPUrlConnection
that is a childreon ofUrlConnection
and has this methodthat should be used to set parameters of the request (like HEADER field).. check here for reference
您还可以看到 URLConnection。
https://docs.oracle.com/ javase/1.5.0/docs/api/java/net/URLConnection.html
You can also see URLConnection.
https://docs.oracle.com/javase/1.5.0/docs/api/java/net/URLConnection.html
如果您绝对必须自己手动完成,则必须遵循此格式,每个标题各占一行。
查看 HTTP 规范中的标头格式。
http://www.w3. org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#Message-Headers
If you absolutely have to do it yourself by hand it must follow this format with each header on its own line.
Look into the header format in HTTP spec.
http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#Message-Headers