Skip to content

Instantly share code, notes, and snippets.

@kiyokura
Created April 4, 2017 15:02
Show Gist options
  • Save kiyokura/88b716344b09e339a86a7917ecf4670c to your computer and use it in GitHub Desktop.
Save kiyokura/88b716344b09e339a86a7917ecf4670c to your computer and use it in GitHub Desktop.
自動構成スクリプトを考慮してOSのプロキシ設定をとってくるコード
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var target = "https://hoge.fuga.example.com/hogehoge/";
var proxyInfo = CheckProxy(target);
}
private ProxyInfo CheckProxy(string targetAddress)
{
var targetUri = new Uri(targetAddress);
var proxyUri = (WebRequest.GetSystemWebProxy()).GetProxy(new Uri(targetAddress));
if (proxyUri.Host == targetUri.Host)
{
// ターゲットとPROXYのHostが同じならProxy無しと判断。
return new ProxyInfo(false, null);
}
else
{
// PROXYアリの場合はそのままセット
return new ProxyInfo(true, proxyUri);
}
}
public class ProxyInfo
{
public bool IsUse { get; set; }
public Uri Uri { get; set; }
public ProxyInfo()
{
}
public ProxyInfo(bool isUse, Uri uri)
{
IsUse = isUse;
Uri = uri;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment