如何在Flurl中将代理用于Web请求? [英] How can I use proxies for web requests in Flurl?

查看:415
本文介绍了如何在Flurl中将代理用于Web请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Flurl客户端有一个简单的发布请求,我想知道如何使用代理使用IP,端口,用户名和密码等信息发出此请求.

I have a simple post request using the Flurl client, and I was wondering how to make this request using a proxy using information like the IP, port, username, and password.

string result = await atc.Request(url)
    .WithHeader("Accept", "application/json")
    .WithHeader("Content-Type", "application/x-www-form-urlencoded")
    .WithHeader("Host", "www.website.com")
    .WithHeader("Origin", "http://www.website.com")
    .PostUrlEncodedAsync(new { st = colorID, s = sizeID, qty = 1 })
    .ReceiveString();

推荐答案

我在寻找类似的答案,发现了这一点: https://github.com/tmenier/Flurl/issues/228

I was looking for a similar answer and found this: https://github.com/tmenier/Flurl/issues/228

这是该链接的内容的副本.它对我有用!

Here is a copy of the contents of that link. It worked for me!

您可以使用自定义工厂来做到这一点:

You can do this with a custom factory:

using Flurl.Http.Configuration;

public class ProxyHttpClientFactory : DefaultHttpClientFactory {
    private string _address;

    public ProxyHttpClientFactory(string address) {
        _address = address;
    }

    public override HttpMessageHandler CreateMessageHandler() {
        return new HttpClientHandler {
            Proxy = new WebProxy(_address),
            UseProxy = true
        };
    }
} 

要在启动时进行全局注册:

To register it globally on startup:

FlurlHttp.Configure(settings => {
    settings.HttpClientFactory = new ProxyHttpClientFactory("http://myproxyserver");
});

这篇关于如何在Flurl中将代理用于Web请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆