Как отправить заголовок с помощью HTTP-запроса через вызов curl?

Это работает:

com / example / model / BearExtra.java

package com.example.model;

public class BearExtra {
  public static void go() {
    System.out.println("Yay, it works!");
  } 
}

com / example / web / Bear.java

package com.example.web;

import com.example.model.*;

public class Bear {
  public static void main(String[] args) {
    BearExtra.go();
  }
}

] Теперь, чтобы скомпилировать и запустить эти классы, перейдите в каталог, в котором вы можете «увидеть» папку com и выполните:

* nix / MacOS

javac -cp . com/example/model/*.java com/example/web/*.java
java -cp . com.example.web.Bear 

Windows

javac -cp . com\example\model\*.java com\example\web\*.java
java -cp . com.example.web.Bear 

, и на консоль печатается следующее:

Yay, it works!
1261
задан gonzobrains 20 April 2015 в 18:09
поделиться

3 ответа

man curl :

   -H/--header <header>
          (HTTP)  Extra header to use when getting a web page. You may specify
          any number of extra headers. Note that if you should  add  a  custom
          header that has the same name as one of the internal ones curl would
          use, your externally set header will be used instead of the internal
          one.  This  allows  you  to make even trickier stuff than curl would
          normally do. You should not replace internally set  headers  without
          knowing  perfectly well what you're doing. Remove an internal header
          by giving a replacement without content on the  right  side  of  the
          colon, as in: -H "Host:".

          curl  will  make sure that each header you add/replace get sent with
          the proper end of line marker, you should thus not  add  that  as  a
          part  of the header content: do not add newlines or carriage returns
          they will only mess things up for you.

          See also the -A/--user-agent and -e/--referer options.

          This option can be used multiple times to add/replace/remove  multi-
          ple headers.

Пример:

curl --header "X-MyHeader: 123" www.google.com

Вы видите запрос, которые завихряются отправленные путем добавления -v опция.

1868
ответ дан Daniel Stenberg 20 April 2015 в 18:09
поделиться
  • 1
    @ATaylor: положительная сторона. Отредактирует – steffen 29 July 2012 в 20:43

Используйте -H or --header .

Страница справочника: http://curl.haxx.se/docs/manpage.html#-H

54
ответ дан Community 20 April 2015 в 18:09
поделиться

В PHP :

curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue'));

или вы можете установить несколько:

curl_setopt($ch, CURLOPT_HTTPHEADER, array('HeaderName:HeaderValue', 'HeaderName2:HeaderValue2'));
277
ответ дан 19 December 2019 в 20:15
поделиться
Другие вопросы по тегам:

Похожие вопросы: