Linux 使用 cURL 执行 HTTP 请求(使用 PROXY)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9445489/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:49:09  来源:igfitidea点击:

performing HTTP requests with cURL (using PROXY)

linuxcurlproxy

提问by user873286

I have this proxy address: 125.119.175.48:8909

我有这个代理地址: 125.119.175.48:8909

How can I perform a HTTP request using cURL like curl http://www.example.com, but specifying the proxy address of my network?

如何使用 cURL 执行 HTTP 请求curl http://www.example.com,但指定我的网络的代理地址?

采纳答案by airween

General way:

一般方式:

export http_proxy=http://your.proxy.server:port/

Then you can connect through proxy from (many) application.

然后您可以通过(许多)应用程序的代理进行连接。

And, as per comment below, for https:

并且,根据下面的评论,对于 https:

export https_proxy=https://your.proxy.server:port/

回答by Karl Barker

From man curl:

来自man curl

-x, --proxy <[protocol://][user:password@]proxyhost[:port]>

     Use the specified HTTP proxy. 
     If the port number is not specified, it is assumed at port 1080.

回答by Amar

The above solutions might not work with some curl versions I tried them for myself(curl 7.22.0). But what worked for me was:

上述解决方案可能不适用于我自己尝试过的某些 curl 版本(curl 7.22.0)。但对我有用的是:

curl -x http://proxy_server:proxy_port --proxy-user username:password -L http://url

Hope it solves the issue better!

希望能更好的解决问题!

回答by Alejandro Moreno

as an adition to airween, another good idea is to add this into your .bashrc, so you'll be able to switch from non proxied to proxied environment:

作为 airween 的补充,另一个好主意是将它添加到您的 .bashrc 中,这样您就可以从非代理环境切换到代理环境:

alias proxyon="export http_proxy='http://YOURPROXY:YOURPORT';export https_proxy='http://YOURPROXY:YOURPORT'"
alias proxyoff="export http_proxy='';export https_proxy=''"

WHERE YOURPROXY:YOURPORT is exactly that, your ip and port proxy :-).

您的代理服务器在哪里:您的端口就是您的 IP 和端口代理:-)。

Then, simply doing

然后,简单地做

proxyon

your system will start to use the proxy, and just the opposite with:

您的系统将开始使用代理,而与此相反:

proxyoff

回答by Filipe Correia

Beware that if you are using a SOCKS proxy, instead of a HTTP/HTTPS proxy, you will need to use the --socks5switch instead:

请注意,如果您使用的是 SOCKS 代理,而不是 HTTP/HTTPS 代理,您将需要使用--socks5开关:

curl --socks5 125.119.175.48:8909 http://example.com/

You can also use --socks5-hostnameinstead of --socks5to resolve DNS on the proxy side.

您还可以使用--socks5-hostname而不是--socks5在代理端解析 DNS。

回答by smitkpatel

you can use :

您可以使用 :

curl http://www.example.com --proxy http://125.119.175.48:8909

as explained by Karl

正如卡尔解释的那样

回答by 13krn

use the following

使用以下

curl -I -x 192.168.X.X:XX http://google.com

curl -I -x 192.168.X.X:XX http://google.com

192.168.X.X:XXput your proxy server ip and port.

192.168.X.X:XX把你的代理服务器ip和端口。

-vverbose mode it will give more details including headers and response.

-v详细模式它将提供更多详细信息,包括标题和响应。

回答by kenorb

For curlyou can configure proxy in your ~/.curlrc(_curlrcon Windows) file by adding proxyvalue, the syntax is:

因为curl您可以通过添加值在~/.curlrc_curlrc在 Windows 上)文件中配置代理proxy,语法是:

proxy = http://username:password@proxy-host:port

回答by overthink

You don't need to export the http[s]_proxyshell variable if you're just setting the proxy for a one off command. e.g.

http[s]_proxy如果您只是为一次性命令设置代理,则不需要导出shell 变量。例如

http_proxy=http://your.proxy.server:port curl http://www.example.com

That said, I'd prefer curl -xif I knew I was always going to use a proxy.

也就是说,curl -x如果我知道我总是会使用代理,我会更喜欢。

回答by Pedro R. Sánchez A.

With a proxy with authentication I use:

使用带有身份验证的代理,我使用:

curl -x <protocol>://<user>:<password>@<host>:<port> --proxy-anyauth <url>

because, I don't know why curl doesn't use/catch http[s]_proxyenvironment variables.

因为,我不知道为什么 curl 不使用/捕获http[s]_proxy环境变量。