【问题标题】:How to set the port in HTTPartyHTTParty中如何设置端口
【发布时间】:2012-10-29 15:42:56
【问题描述】:

我读到 HTTParty 使用 SSL if the port is set to 443,但我不知道如何设置端口。有人可以帮我澄清一下吗?

【问题讨论】:

    标签: ruby ssl httparty


    【解决方案1】:

    检查规格:

    https://github.com/jnunemaker/httparty/blob/82a90c1b93b076f9d2d7410123c2b5d609401a1f/spec/httparty/request_spec.rb#L41

    目标 URL 应使用端口 443。只需在目标 URI 末尾添加 :443 就足以使 HTTParty 使用 SSL。

    顺便说一句,HTTPS URL 也将使用 SSL。

    例子:

    http://foo.com     => no SSL
    https://foo.com    => SSL
    http://foo.com:443 => SSL
    

    【讨论】:

    • +1。最好在 URL 中指定https://,因为端口会自动默认为 443。
    • 同意@MarkThomas。很高兴知道您可以同时使用这两种方法。
    【解决方案2】:

    它实际上默认使用HTTPS,除非端口是80并且请求是HTTP:

    context "using port 80" do
      let(:uri) { URI 'http://foobar.com' }
      it { should_not use_ssl }
    end
    
    context "using port 443 for ssl" do
      let(:uri) { URI 'https://api.foo.com/v1:443' }
      it { should use_ssl }
    end
    
    context "https scheme with default port" do
      it { should use_ssl }
    end
    
    context "https scheme with non-standard port" do
      let(:uri) { URI 'https://foobar.com:123456' }
      it { should use_ssl }
    end
    

    https://github.com/jnunemaker/httparty/blob/master/spec/httparty/connection_adapter_spec.rb

    这是我的规格:

    describe Foo do
      it 'is https' do
        adapter = HTTParty::ConnectionAdapter.new(URI(subject.base_uri))
        expect(adapter.connection.use_ssl?).to eq(true)
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2020-05-25
      • 1970-01-01
      • 1970-01-01
      • 2017-07-29
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 2012-11-11
      相关资源
      最近更新 更多