Java中HTTP请求中添加HEADER

发布于 2024-08-20 03:49:50 字数 556 浏览 7 评论 0原文

我使用以下代码发送简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

以可爱出名 2024-08-27 03:49:50

不要尝试自己实现 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)

等风来 2024-08-27 03:49:50

当您写入

writer.print ("GET " + szUrl + " HTTP/1.0\r\n\r\n"); 

\r\n\r\n 位时,会发送一个换行/回车符来结束该行,然后发送另一个换行符/回车符来指示不再有标头。这是 HTTP 和电子邮件格式的标准,即空行表示标头的结束。为了添加额外的标头,您只需要在完成之前不要发送该序列。您可以执行以下操作

writer.print ("GET " + szUrl + " HTTP/1.0\r\n"); 
writer.print ("header1: value1\r\n"); 
writer.print ("header2: value2\r\n"); 
writer.print ("header3: value3\r\n"); 
// end the header section
writer.print ("\r\n"); 

When you write

writer.print ("GET " + szUrl + " HTTP/1.0\r\n\r\n"); 

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

writer.print ("GET " + szUrl + " HTTP/1.0\r\n"); 
writer.print ("header1: value1\r\n"); 
writer.print ("header2: value2\r\n"); 
writer.print ("header3: value3\r\n"); 
// end the header section
writer.print ("\r\n"); 
給妳壹絲溫柔 2024-08-27 03:49:50

即使我建议尝试 Bozho 提到的 HttpComponents 而不是自己实现 HTTP,这也是添加自定义标头的方法:

 writer.print ("GET " + szUrl + " HTTP/1.0\r\n"); 
 writer.print ("X-MyOwnHeader: SomeValue\r\n");

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:

 writer.print ("GET " + szUrl + " HTTP/1.0\r\n"); 
 writer.print ("X-MyOwnHeader: SomeValue\r\n");
扎心 2024-08-27 03:49:50

您应该使用已经准备好用于 http 连接的类,例如 HTTPUrlConnection ,它是 UrlConnection 的子级,并且具有

void setRequestProperty(String key, String value)

用于设置请求参数的方法(像 HEADER 字段)..检查 此处供参考

You should use classes already prepared to be used for http connections, like HTTPUrlConnection that is a childreon of UrlConnection and has this method

void setRequestProperty(String key, String value)

that should be used to set parameters of the request (like HEADER field).. check here for reference

狠疯拽 2024-08-27 03:49:50

如果您绝对必须自己手动完成,则必须遵循此格式,每个标题各占一行。

名称:值

查看 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.

name: value

Look into the header format in HTTP spec.

http://www.w3.org/Protocols/HTTP/1.0/draft-ietf-http-spec.html#Message-Headers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文