【问题标题】:cURL does not work but same site works with browsercURL 不起作用,但同一站点可与浏览器一起使用
【发布时间】:2016-11-04 18:02:30
【问题描述】:

使用cURL表单linux服务器访问站点返回错误的原因是什么?但在 chrome 中打开此站点成功。

【问题讨论】:

  • 有几个原因,但在我脑海中,我建议这可能是因为您的 cURL 调用不包含页面期望的标题(或 cookie)在请求中。如果您在浏览器的新私人窗口中打开该网址,会发生什么?
  • 有同样的问题。那是因为用户代理和压缩 gzip 不在标头中。
  • 你能提供curl request你犯的错误吗?
  • 我认为问题在于 SSL 错误。谁能告诉我如何在客户端和服务器端修复它
  • @daf 在浏览器中使用私有窗口

标签: linux curl libcurl


【解决方案1】:

在发送请求时查看浏览器标头,并将相同的标头添加到 cURL 请求。有些服务器需要浏览器默认发送的一些标头,而不是 cURL。

【讨论】:

  • 这解决了我的问题。服务器想要一个用户代理标头。
  • 好建议。就我而言,它缺少--digest-c cookies.txt
  • 我必须包含用户代理并接受。这实际上解决了我在使用 Apache HttpClient 时遇到的问题。例外并不是特别有用,所以我为此苦苦思索了好几天。谢谢。
【解决方案2】:

要避免/抑制 SSL 错误,请使用-k 标志,即

curl -k ...

【讨论】:

    【解决方案3】:

    我在 curl 上遇到 301 Moved Permanently 错误,但正在浏览器上工作。

    但是当我看得更深时,有Location 移动到该位置,这将起作用。这可能是在服务器使用 TLS 时。

    $ curl -v http://www.shaharma.com/location/v1/US/zipcode/98104
    *   Trying 172.111.99.100...
    * TCP_NODELAY set
    * Connected to www.shaharma.com (172.111.99.100) port 80 (#0)
    > GET /location/v1/US/zipcode/98104 HTTP/1.1
    > Host: www.shaharma.com
    > User-Agent: curl/7.54.0
    > Accept: */*
    > 
    < HTTP/1.1 301 Moved Permanently
    < Content-Length: 0
    < Location: https://www.shaharma.com/location/v1/US/zipcode/98104
    < Cache-Control: max-age=0
    < Expires: Fri, 01 Dec 2017 19:40:14 GMT
    < Date: Fri, 01 Dec 2017 19:40:14 GMT
    < Connection: Keep-Alive
    < Set-Cookie: UID=52679eee-c06c-49fc-893a-69fd7e46bad0; expires=Fri, 25-Feb-2028 19:40:14 GMT; path=/; domain=.shaharma.com
    < Set-Cookie: SID=1ada5fa9-0ace-4f4e-b75b-7a756b8da934; path=/; domain=.shaharma.com
    < Set-Cookie: shaharma_loc_lb=p-loc-w; expires=Fri, 01-Dec-2017 19:50:14 GMT; path=/; domain=.shaharma.com
    < Set-Cookie: bby_rdp=l; expires=Sat, 02-Dec-2017 19:40:14 GMT; path=/; domain=.shaharma.com
    < 
    * Connection #0 to host www.shaharma.com left intact
    

    另外,您可以进入浏览器查看包含标头信息的网络。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多