Ruby Proxy GET / POST authentication using OpenURI or net / http

I am using ruby ​​1.9.3 and trying to use open-uri to get the url and try to send using Net:HTTP

I am trying to use proxy authentication for both:

Trying to execute a POST request using net/http :

 require 'net/http' require 'open-uri' http = Net::HTTP.new("google.com", 80) headers = { 'User-Agent' => 'Ruby 193'} resp, data = http.post("/", "name1=value1&name2=value2", headers) puts data 

And for open-uri , which I cannot do POST , I use:

 data = open("http://google.com/","User-Agent"=> "Ruby 193").read 

How do I change them to use a proxy with HTTP authentication

I tried (for open-uri)

 data = open("http://google.com/","User-Agent"=> "Ruby 193", :proxy_http_basic_authentication => ["http://proxy.com:8000/", "proxy-user", "proxy-password"]).read 

However, all I get is OpenURI::HTTPError: 407 Proxy Authentication Required . I checked everything and it works in the browser with the same authentication and proxy settings, but I cannot get Ruby to do this.

How do I change the code above to properly add HTTP authentication? Has anyone gone through this atrocity?

+6
source share
1 answer

Try:

 require "open-uri" proxy_uri = URI.parse("http://proxy.com:8000") data = open("http://www.whatismyipaddress.com/", :proxy_http_basic_authentication => [proxy_uri, "username", "password"]).read puts data 

As for Net :: HTTP, I recently implemented proxy support with http authentication in a Net :: HTTP wrapper library called http . If you look at my last pull request, you will see the main implementation.

EDIT: Hope this makes you move in the right direction.

 Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port,"username","password").start('whatismyipaddress.com') do |http| puts http.get('/').body end 
+14
source

Source: https://habr.com/ru/post/912591/


All Articles